Laravel route parameters don't care at all - laravel

I come from here:
Laravel pagination trouble with offset
Investigating into my code, I found that when a random alike parameter is given through a route, it will do an like query or I don't know, but it will still work as it will get the first part of the parameter:
The route I'm trying to "ensure" goes like this:
Route::get('/videos/cats/{race_id}/{cat_id}, [App\Http\Controllers\MeowController::class, 'getCatVideo']);
Can someone help me to get that exact parameter as integer, or prevent a route from working if the parameter is not exactly the given one?
Thank you in advance.

Parameters are required by default.
To validate parameter:
Route::get('/videos/cats/{race_id}/{cat_id}, [App\Http\Controllers\MeowController::class, 'getCatVideo'])
->where('race_id', '\d+')
->where('cat_id', '\d+');
Useful link: https://laravel.com/docs/8.x/routing#parameters-regular-expression-constraints

Related

Laravel - How do I navigate to a url with a parameter to left of subdirectory

I have to test this Route, but I am not sure how to navigate to it.
Route::get('/{type}/Foo/{page}/{date}', 'FooController#index');
I understand that URLs usually have subdirectories defined before the parameters in the URL.
I have worked with URLs like this example
Route::get('/Foo/{type}', 'FooController#index');
which would have an endpoint that looks like
/Foo?type=bar
Does anybody know how to test a route such as the one above?
Well i think that you need to clear out a bit the difference between route and query parameters.
In your case you are trying to use route parameters which will actually look something like:
/{type}/Foo/{page}/{date} => /myType/Foo/15/12-11-2021
Laravel considers the words inside {} as variables that you can retrieve via request so you can do something like:
$request->type
and laravel will return you the string myType as output.
In your second case that you have tried in the past you are referring to query parameters which are also a part of the $request. Consider it as the "body" of a GET request but i don't mean in any way to convert your post routes to GET :)
The thing with your second url is that:
/Foo/{type} is not similar to /Foo?type=bar
instead it should be like: /Foo/bar
In general query parameters are when you want to send an optional field most of the times in your GET endpoint (for filtering, pagination etc etc) and the route parameters are for mandatory fields that lead to sub-directories for example /Foo/{FooId}/Bar/{BarId}
The thing to remember is that you must be careful about your routes because variables can conflict with other routes.
For example a route looking like this:
Route::get('/foo/{fooId}', [FooController::class, 'getFoo']);
Route::get('/foo/bar', [BarController::class, 'getBar']);
will conflict because laravel will consider bar as the variable of the fooId so your second route can never be accessed.
The solution to this is to order your routes properly like:
Route::get('/foo/bar', [BarController::class, 'getBar']);
Route::get('/foo/{fooId}', [FooController::class, 'getFoo']);
So when you give as a route parameter anything else than bar your will go to your second route and have it working as expected.

How to remove parameters from url in laravel?

I am trying to remove the url parameters based on some conditions ,i can able to remove some of the parameters , i want to replace the some new value for the url, can you please give me some idea how to do this one
$request->query->remove('key');
$request->query->add(['admin'=>"true"]);
$request->request->remove('key');
$request->request->add(['admin'=>"true"]);
You almost done in a correct way, simply change the order of the queries then it will be working fine.
$request->query->remove('key');
$request->request->remove('key');

How to get an array from GET URL parameters in Laravel

Hello to everyone who is seeing this! I am a Laravel beginner, using Laravel for API-s calls. I cannot find an appropriate solution how to pick up parameters from GET method that looks like this:
http://localhost:8000/api/products/searchv2/cat=category1&cat=category2
Now I want to receive in my Laravel controller this array
$categories = ["category1", "category2"]
I think the solution is very easy, but due to minimal experience, I cannot find the right answer on the internet or Laravel documentation. Thanks to everyone who tried to help me!
According to this answer, you can use the [] syntax like you would with form fields to create an array. So in your case, the URL would be:
http://localhost:8000/api/products/searchv2/?cat[]=category1&cat[]=category2
And $_GET['cat'] should return an array, so in theory $request->get('cat') would also return an array.
Using Commas
Another way would be to make use of commas, e.g.:
?cat=category1,category2,category3
You could then use explode:
$categories = explode($request->get('cat'));

how route works in laravel with parameter

Maybe this is a very basic question but,
In laravel if i use this route:
Route::get('/{campo}','ItemController#show');
and then i try to use this route
Route::get('/mondo/','ItemController#mondo');
I get simply redirect to a ItemController#show with parameter 'mondo' but I can't reach ItemController#mondo because he take mondo like a parameter. How can I let laravel know when i want he take a variable from url and when i don't?
You just have to change the order:
Route::get('mondo','ItemController#mondo');
Route::get('{campo}','ItemController#show');
Laravel takes what comes first and your {campo} route was accepting every word you had in your URL.
Just flip your order.
first route to second and second one to first one

Trouble with Codeigniter Routes involving a query

I'm having a little trouble with a CodeIgniter route when there is a query (stuff after the ?) in the URI. I know it is good practice to replace queries with routes in CI, but I'm importing in a premade messageboard that already does everything with queries. This is my route:
$route['messageboard/:any'] = "messageboard/index";
Any in this case refers to a script name. So if it's messageboard/admin.php, I have it load a view that loads my premade messageboard's script "admin.php". It's working just fine if I do messageboard/admin.php. It does fine if I do messageboard/admin.php?. If I put a parameter into the query, however, the route won't correctly send the user to the messageboard controller, and instead sends them to a 404. Does anyone have any ideas on how to make this work? I would be eternally grateful. Thanks!
Okay guys, I solved it. I needed to change three things. The first was mtvee's suggestion, which lets it read query strings. The second one you're going to want to change the $config['permitted_uri_chars'] in the config file to include an equals sign, since it starts off disabled and all query strings will be of the for ?a=34 or something like that. The third is you need to go to $config['uri_protocol'] and change it from AUTO to PATH_INFO. Once I did those, it worked.
I'm sure the syntax is:
$route['messageboard/(:any)'] = "messageboard/index"; //<-- notice brackets
and not
$route['messageboard/:any'] = "messageboard/index";
I believe CI doesn't do GET out of the box. Check out Enabling Query Strings here http://ellislab.com/codeigniter/user-guide/general/urls.html

Resources