how route works in laravel with parameter - laravel

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

Related

Laravel route parameters don't care at all

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

Laravel route with & without parameter

The question might sound simple but what's the difference between those 2 routes exactly ?
why dont we need to Add parameter for the first route
Routes
Route::get('/products', [ProductController::class, 'index'])->name('product.index');
Route::get('/products/{slug}', [ProductController::class, 'show'])->name('product.show');
here in blade file
Low to High
High to Low
When you pass an array to the route helper it will try to match up the keys of that array to the route parameters (if there are any to match up), anything that isn't a route parameter gets added as a query string parameter; so that array isn't just for route parameters.
Your index route shows all elements which is not a specific element.
The show route expects a parameter to show you all details of exactly one item.

Product And Category Separation In Route (Laravel)

I'm setting up a new route system.
Route::get('/{cat1Url}', 'CategoryController#showCat1')->name('showCat1');
Route::get('/{productUrl}', 'ProductController#showProduct')->name('showProduct');
My sef link is after "/"
But,
{{ route('showProduct',[$p->pr_url]) }}
This method not working with route name. Working only upside route.
I don't want use
"/cat/myVariable"
or
"/product/myVariable"
Can't I use route name to work this way?
What is the solution to this?
In this way, if you make a get request to /something the laravel you start from top of web.php file looking to a route that follows the pattern. Your both routes will follow that pattern, but the laravel will always, pass the first one to controller.
You have two options:
Put only one route, and inside the controller you switch to the appropriate function. But this isn't a great ideia, because this is the function of the Web.php.
Use the routes like the documentation recommend:
Route::get('/cat/{catId}', 'CategoryController#showCat')->name('showCat');
Route::get('/prod/{productId}', 'ProductController#showProduct')->name('showProduct');
and in Controller you make the appropriate handler of your Category or Product.
You will have to have a way to tell Laravel which url to be mapped to what otherwise it will always use the last defined route. So in your case calling /myVariable and /myVariable it will use the latest definition which is showProduct. The only other way is if you use regular expression to differentiate the variables. For example:
Route::get('/{cat1Url}', 'CategoryController#showCat1')
->name('showCat1')->where('cat1Url', 'cat-*');
Route::get('/{productUrl}', 'ProductController#showProduct')
->name('showProduct')->where('productUrl', 'prod-*');
This way your slugs need to start with what you define, but you cannot use just id as a numeric value for both.

How to use id and /route in Laravel?

I have 2 routes:
/route/{id}
/route/add
First one shows something based on id provided and second one provies form for adding news item. My problem is that when I hit /route/add it takes the add word and treats it as id for the first route.
Is there any solution for this in laravel or do I have to use different route names?
Routes work on a first-come first-served basis. The earlier routes are looked at first. Since you have a wildcard {id} on your first of those two routes, Laravel is treating add as {id} and will be passing off to that controller/closure.
You should switch the two routes around like this:
Route::get('route/add', 'Controller#method');
Route::get('route/{id}', 'Controller#method');
OR you can always add a filter to the first route in order to tell Laravel that {id} should be a number:
Route::get('route/{id}', 'Controller#method')->where('id', '[0-9]+');
Route::get('route/add', 'Controller#method');
This way, Laravel will try to match add to your {id} wildcard but it will fail because add is not a number.
Please replace the two lines:
/route/add
/route/{id}
The {id} parameter is not always expecting a numeric value. A string is also valid like "add".

Codeigniter routes don't work correctly

I have two string in routing config.
$route['education/course/(:any)'] = "education/course/$1";
$route['education/course/(:any)/(:num)'] = "education/lection/$1/$2";
But when I went to /education/course/my_course/1, the first rule worked, but the second didn't.
Please help! I'm newbie in CI.
Routes run in the order they are defined. Your second one will never be applied because the (:any) wildcard is capturing, well, anything.
I believe you should be able to switch the order so the most specific is first, followed by the least specific:
$route['education/course/(:any)/(:num)'] = "education/lection/$1/$2";
$route['education/course/(:any)'] = "education/course/$1";
Since both two routes are similar in the first three segments
education / course / (:any)
And since Route.php runs procedural (line by line),
Requesting a page like /education/course/my_course/1 matches the first route pattern (below)
$route['education/course/(:any)'] = "education/course/$1";
And also, requesting a page like /education/course/my_course/1/23 will still matches the first route pattrn, because Route.php only cares if your requested URL link matched the specified route pattern or not, otherwise go check the next route.
So, switching the order of the routes will fix the problem.

Resources