Omar Makled
Omar Makled

Reputation: 147

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

Upvotes: 0

Views: 1124

Answers (1)

lukasgeiter
lukasgeiter

Reputation: 152860

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', '(.*)');

Upvotes: 8

Related Questions