This question already has answers here:
Optional parameter in the middle of a route
(3 answers)
Closed 3 years ago.
I'm having trouble creating ONE laravel route that has an optional parameter. The following achieves the behaviour I want:
Route::get('/{locale}/donate', 'MyController#index')->name('donation.index');
Route::get('/donate', 'MyController#index')->name('donation.index');
Both the urls /fr/donate and /donate will load the MyController index(). However, when I do this:
Route::get('/{locale?}/donate', 'MyController#index')->name('donation.index');
The /donate will not load the MyController index(). How do I make the locale argument an optional segment in the url?
optional parameters can be located only at the end of the url
Route::get('/donate/{locale?}', 'MyController#index')->name('donation.index');
In my opinion there is no way to define the parameter as optional. Here are other ways to solve your problem.
First
Create a subdomain for that
Second move your optional parameter to the last. Now your route will look like this
Route::get('/donate/{locale?}', 'MyController#index')->name('donation.index');
Related
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
This question already has answers here:
Get Laravel 5 controller name in view
(8 answers)
Closed 3 years ago.
I wonder if how i can get the Controller name and Action name in the Blade template/View?
Thanks in advance!
A few options.
Pass variables to the view from your controller.
Set a global view variable
use view composer.
You need to choose one of these depending on your use case.
Route::getCurrentRoute()->getAction();
Route::currentRouteAction();
Route::currentRouteName();
I would like to use a URL as route parameter in Laravel. Here is the route declaration from my web.php
<?php
Route::get('/url/{id}/{url?}',
'URLController#find')
->where('url', '(.*)');
This is working fine if the "url" parameter doesn't have its' own query parameters. Yet this is not working as expected:
http://www.somedomain.com/productsdetails?prodid=44296
When I pass the above URL, it just get the http://www.somedomain.com/productsdetails part of the parameter. Laravel couldn't resolve the part after the question mark.
Do you know any workaround for this?
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
I'm having an issue with Laravel 4 routing. I am trying to create two sets of routes:
domain.com/meetings/aa, al-anon, etc.
domain.com/meetings/day/sun, mon, tue, etc.
Here is what I am specifying in my routes file:
Route::resource('meetings/day/{dayName}', 'Meetings_DayController');
Route::resource('meetings/{fellowshipName}', 'Meetings_MeetingController');
I need to be able to pass variables to my resourceful controllers. But I am getting this kind of an error, no matter what order I put the routes in:
Route pattern "/meetings/fellowship/{fellowshipName}/{{fellowshipName}}" cannot
reference variable name "fellowshipName" more than once.
Not only that, but those two routes cause other, undeclared routes to give the same error. My 404 route doesn't kick in for some reason. Ideas, anyone? I've tried everything I can think of. If I could match a three-letter string with the day controller route, that would work, but I can't figure out the regex for a three-letter string in Laravel. \w{3} doesn't work.
You are using Route::resource incorrectly. You can't (and should not) pass a variable to a resource controller. Instead You will need to declare a new route with the verb you need and the parameter. Route::resource only creates several pre-handled routes for you for quick CRUD RESTful access
See the answer to similar situation here: https://stackoverflow.com/a/19608572/385402