F
Filament3mo ago
WEBMAS

How to make optional not only a parameter but also a separator in Route::get()?

Hello everybody. I have a route in routes/web.php:
...
Route::get('/account/{user}/course/{course?}/lesson/{lesson?}', 'index');
...
...
Route::get('/account/{user}/course/{course?}/lesson/{lesson?}', 'index');
...
?} - means that the parameter is optional Example links: /account/123/course/123/lesson/123 - correct /account/123/course/123/lesson - correct /account/123/course/123 - not correct How to make optional not only {lesson?} but also the entire part of it /lesson/{lesson?} including /lesson ? This is not a Filament question.
3 Replies
WEBMAS
WEBMAS2mo ago
Is this possible to do or not?
toeknee
toeknee2mo ago
Route::get('/account/{user}/course/{course}', 'YourController@index')->name('course.index');
Route::get('/account/{user}/course/{course}/lesson/{lesson?}', 'YourController@index')->name('course.lesson.index');
Route::get('/account/{user}/course/{course}', 'YourController@index')->name('course.index');
Route::get('/account/{user}/course/{course}/lesson/{lesson?}', 'YourController@index')->name('course.lesson.index');
With these two routes, the first route will match the /account/{user}/course/{course} pattern, and the second will match the /account/{user}/course/{course}/lesson/{lesson?} pattern. Both routes can point to the same controller method if your logic for handling them is similar.?
WEBMAS
WEBMAS2mo ago
The solution to split one route into different routes works. This is what I did in my project. But I want to combine them into one route. I've read a lot of documentation and it doesn't seem possible to do this. This was possible to do in Lumen, because it used a different engine for processing routes.
Want results from more Discord servers?
Add your server