CodeIgniter routing problem. (appends ajax route to existing url) - ajax

I'm trying to perform an AJAX-request in a view, the user gives some input which is sent to the server with AJAX and the function it's supposed to go to is routed with CodeIgniters routes.
This is the view I'm currently standing in while making the request.
http://localhost:8888/companies/list
In my route config I've set this route below to handle the AJAX-request, which should be able to come from any view and still be able to go to the route I've specified.
$route['test_ajax'] = "ajax/test_ajax";
So the request should go to the "ajax"-controller and use the function "test_ajax", which should make the POST-url look like this.
POST http://localhost:8888/test_ajax
But instead what I get is the current URL I'm standing at, and the route I've specified appended to the URL crashing my response from the AJAX-request completely since it didn't even go close to the function it's supposed to. The POST-url I get looks like this.
POST http://localhost:8888/companies/test_ajax
Notice how the parameter of /companies was removed. The argument /list was lost somewhere, al though if I add a trailing slash after the list I get the list argument in the URL as well.
So what just happened is that the POST tries to go to the companies-controller and look for the function test_ajax which is defined in the ajax-controller and not in the companies-controller. This error keeps occuring no matter what URL I'm at, and it always follows the same pattern. It keeps appending my route-URL to the existing URL instead of routing correctly.
So what could be causing the routing to behave this way, is there any setting that's accidently enabled or anything? Because I know I've got this to work hundreds of times in previous projects.
Thanks in advance.

It is because your Javascript is using the current directory as the base, and appending the AJAX URL to it. Because you are (to the client-side at least) in the companies directory, it appends your URL onto this.
The solution, if your Javascript is inline, is to just use the base_url() PHP function wihtin the code ...
var url = '<?= base_url(); ?>test_ajax/'
If your Javascript is not inline, you can declare a global variable at the top of your HTML document using the PHP function...
var BASE_URL = '<?= base_url(); ?>'
And use it everywhere else in your Javascript ...
var url = BASE_URL + 'test_ajax/'
Alternatively, you could just hardcode your base URL, but that could get real messy real quick.

Turns out, CodeIgniter interpreted this as a relative link due to the fact that there was no heading slash. CodeIgniter User-Guide states that no heading or trailing slashes should be written in the routes config.
What solved this though was adding a heading slash in the java-URL.
$.ajax({
url: "/test_ajax",
type: "POST",
data: data,
success: function(data){
console.log(data);
}
});
This forces CI to interpret this as a absolute URL and gives me the URL I was looking for.
POST http://localhost:8888/test_ajax

Related

Laravel Routes - Possible to reuse ajax route call from one form on different form?

I have an existing route I created to perform an ajax call for a chained select:
Route::get('add/{id}','AddAssetController#getModel');
The above queries the db for models belonging to each manufacturer.
The url part of the ajax call (man_ID is the id of the manufacturer from the select):
url: 'add/' + man_ID,
The above works perfectly on the main form where I am using it. However, I've found that I need this chained select on more than one form. Is there a way to be able to use the same route for two different pages? I've tried calling it from a different page and in the console, I'm getting a 404 error.
Am I missing something? Is this possible?
You are probably calling the script from the differently nested pages, thus add/ is ultimately calling different path.
I would strongly advise you name your routes, and generate links like that:
Route::get('add/{id}','AddAssetController#getModel')->name('get-model');
and in your blade file
url: '{{route('get-model', ['id' => $id])}}',
if you still set on using paths to call your script, use them relative to your root:
url: '/add/' + man_ID,

how to get full url with # in laravel

I am using laravel 5 and I am trying to get my full url which is
/faq#manual
However I can only get /faq
when I call the function
Request::url() or Request::fullUrl()
is there any way I can get the full url
with #manual?
It is impossible to get the fragment from a URL with Standard HTTP. So using variables like $_SERVER or $_GET can't help you. Actually, the server never knows about this value because the browser won't even send a request with a fragment part.
If you want to have the fragment value in the server side, you should get the value by javascript first then pass it as a URL parameter and get it by $_GET in your server side.
That's not possible in http or php however you can use javascript to get the full url including the hash.
var url = document.URL;
or
var url = window.location.href;
Hope this helps.

Best way to pass url as parameter to route

I am trying to pass any full url in as parameter to route, but the slash seems to mess everything up. If a route is passed in encoded, the route seems to decode it, is there a way to stop this or encode the url again at route level?
Route::get('add/{title?}/{url?}', 'HomeController#add')->name('add');
also i have tired
Route::get('add/{title?}/{url?}', 'HomeController#add_')->where('url', '(.*)')->name('add_popup');
but if it comes across question mark in the url it will drop anything after the question mark.
Check this out
Route::get("url/{url}", function($url) {
return $url;
})->where('url', '.*');
Example: http://myapp.test/url/http://try.me.com prints
http://try.me.com
I would recommend Jeunes answer, however if you still want a route parameter you could do a base64 encode/decode. This will not make for a pretty url though.
JS
btoa("http://someurl.test")
PHP
Route::get('url/{url}', function ($url) {
return base64_decode($url);
});

Laravel Route::get() function and parameter confusing

Hello everyone I just installed laravel4 and spend two days trying to make the first step. Now I made it but I'm confused about the Route::get() function and his paremeters.
I installe laravel directly in
/opt/lampp/htdocs/laravel
then follow tutorial to create file
userform.php
into app/views, then add following codes into routes.php
Route::get('userform', function()
{
return View::make('userform');
});
. Then I go to
/localhost/laravel/public
to see welcome page, and
/localhost/laravel/public/userform
to see the form defined in the view/userform.php.
Q1: According to chrome dev tools, i see in the html page, the form action is
http://localhost/laravel/public/userform
but there is nothing under public but
index.php, favicon.ico packages robots.txt
Q2: for
Route::get('userform', function()
{
return View::make('userform');
});
what is the first "userform" represent?? according the official tutorial, it's supposed to be url, but what is the former part?
for this line
return View::make('userform')
I guess "userform" referes to the file /app/views/userform.php, right?
The .htaccess file in the public directory is responsible for funnelling all incoming requests through the index.php file. This allows Laravel to grab the URI and match it to the route you defined and eventually return to you the view you made.
So you request localhost/laravel/public/userform, the request is funnelled through index.php and Laravel is booted. Laravel picks off the userform part of the URI and matches it against your defined routes. It finds the route you defined and fires it and returns the response.
You're spot on with what you were thinking with your second question as well. When you call View::make the first argument is the name of the view you want to "make". If you named your view app/views/forms/user.php then you would return it like so in your route:
return View::make('forms.user');
Or you could use a slash:
return View::make('forms/user');

htaccess redirection for #! in urls

I am using ajax to load pages on my website.
Each time a page is loaded I change the url in the browser to
http//www.example.com/old/page/#!/new/page/
by setting it through window.loaction using javascript
Now what I want to do is when someone comes to my website by entering the url
http//www.example.com/old/page/#!/new/page/
he should automatically get redirected to
http//www.example.com/new/page/
This is somewhat that happens on facebook too.
Can someone help me out with the required .htaccess code to achieve the same.
Thanks in advance
I don't think anything past the # symbol in your URL is even visible on the server side. So htaccess, php, etc won't even know the hash is there to begin with. I think in order to pull this off you're going to have to use a client side redirect.
window.onload = function(){
// First we use a regex to check for the #! pattern in the hash
if(window.location.hash.match(/\#\!/i)){
// If we found a match, use substring to remove the #! and do a redirect
window.location = window.location.hash.substring(2);
}
};
This example will redirect the user immediately on page load. Unfortunately doing a redirect in this manner won't help the search engines to reindex your site, but thats just one of the pitfalls of using fancy javascript or hash based URL's.

Resources