Laravel Optional Route Parameters

Route::get('dashboard/{path?}', function($path= null)
{
    return $path;
});

yeah that makes sense

what if url is

dashboard/movies/funny/../..

got NotFoundHttpException

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Per default a route parameter cannot contain any slashes, because multiple route parameters or segments are separated by slashes.

If you have a finite number of path levels you could do this:

Route::get('dashboard/{path1?}/{path2?}/{path3?}', function($path1 = null, $path2 = null, $path3 = null)

However this isn’t very elegant nor dynamic and your example suggests there can be many path levels. You can use a where constraint to allow slashes in the route parameter. So this route will basically catch everything that starts with dashboard
Route::get('dashboard/{path?}', function($path= null){
    return $path;
})->where('path', '(.*)');


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x