Best way to pass url as parameter to route - laravel

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);
});

Related

Removing /profile/ from /profile/{username} keyword from Route Web.php does not work

I have a Route
Route::get('/{username}', 'ProfileController#profile')->name('profile.view');
If I keep it in the middle of the file, all the route after this does not work.
If I keep this in the bottom, then everything works.
Also, If I add any work like Profile, it works.
Route::get('profile/{username}', 'ProfileController#profile')->name('profile.view');
How to solve this?
That's the way it is supposed to work as you are using a wildcard to match everything. So either you put it at the bottom of the file and it will be used as a fallback route, which means nothing above it should match, then it will fallback to that route. Or you can use a regex to match the username to something which makes it different then the other routes, something like:
Route::get('{username}', 'ProfileController#profile')
->name('profile.view')
->where('username', 'YOUR REGEX HERE');
I would go with the one you showed and already works:
Route::get('profile/{username}', 'ProfileController#profile')
->name('profile.view');
// or
Route::get('user/{username}', 'ProfileController#profile')
->name('profile.view');

Codeigniter router get parameter

I use $.getJSON() to retrieve some data for a couple of cascading dropdowns in my form. $.getJSON() automatically appends the parameter at the end of the URL like domain.com/controller/method/?parent=5
So, I've declared my method like public function method($parent) which works file, but the same method will be used from other parts of the website that will call it like domain.com/controller/method/5
I tried to create a route in routes.php like the one below:
$route['business/regions/?parent=(:num)'] = 'business/regions/$1';
but it doesn't seem to work. Am I doing something wrong? Maybe ? is confusing the regex parser of the router? Do I have to escape it somehow to make it a 'literal' ? ?
Or is it that router is not used to 'rewrite' get parameters at all? I'm very confused, as it should work but it doesn't and I'm wondering what's wrong with it...
Codeigniter route parameters are for url parameters. It is particularly useful when trying to create a REST styled url pattern.
What you're trying to do is get url query string from the url which is not supported via the Codeigniter router. For you to get what you want you can do the following:
In your routes.php:
$route['business/regions'] = 'business/regions';
and in your controller Business.php:
public function regions() {
//the numeric id you're looking for
$parent = $this->input->get('parent');
}

Using pound sign (#) in Laravel routing?

Laravel has no problem routing the following URI:
$router->get('demo/toggle.html', function() {
return View::make('ng.demo.toggle');
});
However, this one won't work for some reason.
$router->get('demo#/toggle.html', function() {
return View::make('ng.demo.toggle');
});
Is there a way to make this work?
Everything behind the hashtag (#) isn't send to the server, so Laravel can't catch it when you enter it in the browser. This is where the error comes from, Laravel only gets demo.
You can try this with an existing, working route. Just write
demo/toggle.html#some_gibberish <<< will still take you to demo/toggle.html
I'm wondering why you are using '..../toggle.html' as getter, one of the benefits of (Laravel's) url rewriting is that this is avoidable. You could use only toggle instead.

Rewrite url: serve both query string and block urls

my journey of learning MVC continues and as hard as it is, I'm learning a lot of things I could never learn otherwise. Now I have faced the problem in routing. Currently I'm taking the $_SERVER["REQUEST_URI"] and get the controller and method and any args. When url is in format http://mysite.com/forum/thread/12/1123 there is no problem but I need to catch also requests like http://mysite.com/index.php?forum=12&&thread=1123.
I have read links in threads below but cannot get my head on QSA and I though I would better ask.
Thanks
mod_rewrite: Check for Custom query string in URL?
Rewrite url with query string in htaccess
I ended up writing something like before:
I redirect using htaccess
//No Controller specified in url (The current url is base url like http://example.com/hosanna_framework/)
if(!isset($_GET['base_url'])){
$url = $config["router"]["default_controller"];
}
//Controller is specified in url
else{
$url = $_GET['base_url'];
}

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

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

Resources