Code igniter URL? - codeigniter

URL Helper in CI is so bad thing, i dont know what to do :(
I have called
$this->load->helper('url');
And on view i have called
echo current_url();
I got something like this ??
http://www.example.com/?druga
The real url is
http://www.example.com/druga.html
I think CI is not doing proper way, is there any help?

Related

Redirect two pages back with url()->previous() in laravel

I'm creating an affiliate tracking system with some custom affiliate links. These links trigger a controller function to store some metrics in the database then redirect to the actual page the user intended.
My desired function is this: insert affiliate link into webpage->user clicks->controller fires/data stored->redirect to intended page. One of the pieces of data I want to store is the originating site the user clicked on the link. But because I'm calling the controller with a URL, url()->previous() gets my affiliate link that fired the controller instead of the originating site that has the link embedded.
I'm not sure if "previous()" can accept parameters but i tried "previous(2)" and that obviously didn't work.
$url = url()->previous();
$click->came_from = $url;
Using the code above, if my link was hosted on www.w3schools.com and my affiliate link is myurl/affiliatelink/2, I would want "www.w3schools.com" to be inserted into my database, but i'm getting "myurl/affiliatelink/2" inserted instead. Can anyone help me figure out a way around this?
The previous() method returns the user's last location within the app, not the last URL the browser accessed.
To do that, in Laravel, you can try Request::server('HTTP_REFERER'), or in plain PHP, $_SERVER['HTTP_REFERER'].
That said, be aware that HTTP_REFERER is not exactly the most reliable value. It can be spoofed (faked) easily or not provided at all, so test accordingly.
If possible, the best option would be to add a GET parameter to the link present in the remote site, so that when you receive the request in your Controller, you have the means to identify where the user is coming from.
You can simply get and store the previous url with the parameter in a variable($url) from your blade file, then pass the variable to your anchor tag as shown below:
#php
$url= url()->previous();
#endphp
href="{{url($url)}}"
This works very fine.

How to call the codeigniter controller funcation in model?

I am fairly new to Codeigniter and am trying to call in a function from my model but I cannot get it to work. Can anyone see what I am doing wrong here?

Umbraco AJAX call not working

I have a MVC application where I have installed umbraco 6.1.6 nuget package.Now I am trying to call a controller from a view using Jquery AJAX function. The controller is placed in MVC controller folder and it is inherited with UmbracoApiController. I have tried to follow the Umbraco API document ,But everytime my request is send it redirects to 404 page. I have tried the same using umbraco 7.1.4 downloaded from here and it works fine for me. Can anyone suggest a solution for this.
The request url I have used is as follows :- /Umbraco/Api/[ControllerName]/[ActionName], I have also tried /Umbraco/[YourAreaName]/[YourControllerName] , but that too doesn't work for me.
Check different things:
reboot the site (touch the web.config)
did you BUILD the code? If you put it in app_code is it picked up automatically, otherwise you need the DLL to be in your bin directory.
what do the log files in /App_Data/Logs tell you?
You can insert your own logs by invoking
LogHelper.Info<myclass>("some message")
are you really really really sure you have not overlooked something from the documentation?
are you sure your controller inherits from UmbracoApiController
have you tried adding [MemberAuthorize(AllowAll = true)] to the api controller or the method?
double check the URL. you probably mistyped the name. The controller is the complete name without "controller". Try the code below to find back the exact url.
The code below came from a usercontrol which i used in a dashboard for the end user. I guess it's pretty easy to convert it into a Razor macro. If this is not returning a thing, you did something wrong from the list above.
var requestHandler = HttpContext.Current.Request.RequestContext;
var urlHelper = new UrlHelper(requestHandler);
var actionlink = Umbraco.Web.UrlHelperExtensions.GetUmbracoApiService<MyApiController>(urlHelper, "MyMethodName");

Get Page URL after Ajax call has been made

I am using a ajax call to update page content and update the URL accordingly. I have share buttons on the page and when I want to share the whole page I only receive the previous loaded URL.
So as an example -
http://localhost/labs/category/best-of-the-best-campaign/
is my current loaded URL. When I do a Ajax call
http://localhost/labs/tag/ecommerce/?catid=2
This is the new URL. But when I share the page I still get the previous loaded URL. which is
http://localhost/labs/category/best-of-the-best-campaign
Could anyone point to me what might be going wrong?
<?php echo $url="http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";?>
This is what I'm using to get the current URL of the page. I have a doubt that it might be due to the server request but I am not entirely sure about it and how I might change it.
Any help will be greatly appreciated.
Thanks
I think you should reinitialize the $url variable after ajax call
You can write php code in your ajax sucess function like,
<script>
......// your code
success:function()
{
// your code
<?php
$url="http://localhost/labs/tag/ecommerce/?catid=2";
and use it.
?>
}
......// your code
</script>
Why don't you use location.href to get the url of the current page in JavaScript?

Way to tell if a post came from an ajax call in codeigniter?

I just started using CodeIgniter after using Zend for a while. My new site has a feature where you register through Ajax. In Zend I could use this to check if the incoming POST was through AJAX, and therefore from my site:
if(!$this->getRequest()->isXMLHttpRequest())
Is there a piece of code in CodeIgniter that does the same thing? If I don't make sure it's an AJAX call, someone could theoretically register anything they wanted by creating a form to post to my controller.
Thanks!
Since CodeIgniter 2.0, there is an easier way of checking for an ajax request.
Use: $this->input->is_ajax_request();
Doc: https://codeigniter.com/user_guide/libraries/input.html
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest')) {}
But since you are using codeigniter, its better to use their input class . See how to do it below.
if($this->input->is_ajax_request()){
//Execute Your Code
}
you can check it using
$this->input->is_ajax_request();

Resources