The following is well-defined in the Laravel docs:
Route::get('/users/{userid?}', $callback);
But how do I handle a wildcard on the left hand side of a URI? I want the following routes to all be picked up by one statement:
/about-us
/en/about-us
/fr/about-us
Something like:
Route::get('{language_code?}/about-us', $callback);
However this results in an unresolved redirect, at least in the project I'm working on. In summary, how do I handle and optional left-hand wildcard in Laravel?
No, you cannot have an optional parameter followed by the required pattern.
Also it has been an issue on the Github repo and was closed by Taylor Otwell
https://github.com/laravel/framework/issues/3549
Suggested solutions are:
1) Add the optional parameter at the end.
2) You can have a subdomain for it.
As Balarj said "you cannot have an optional parameter followed by the required pattern"
we can do something diffrent for example:
Route::get('{language_code?}/about-us', function($language_code){
if(!$language_code) {
// if the user has not provided the language_code param
return redirect('/about-us');
}
return 'Language is ' . $language_code;
});
then define the the default
route
Route::get('/about-us', function() {
return 'about-us page';
});