Laravel可选路由参数

Oma*_*led 0 routes laravel

Route::get('dashboard/{path?}', function($path= null)
{
    return $path;
});
Run Code Online (Sandbox Code Playgroud)

是的,这是有道理的

如果网址是什么

dashboard/movies/funny/../..

拿到 NotFoundHttpException

luk*_*ter 7

默认情况下,路由参数不能包含任何斜杠,因为多个路径参数或段由斜杠分隔.

如果你有一个有限数量的路径级别,你可以这样做:

Route::get('dashboard/{path1?}/{path2?}/{path3?}', function($path1 = null, $path2 = null, $path3 = null)
Run Code Online (Sandbox Code Playgroud)

然而,这不是很优雅也不是动态,你的例子表明可以有很多路径级别.您可以使用where约束来允许route参数中的斜杠.所以这条路线基本上会抓住一切开始dashboard

Route::get('dashboard/{path?}', function($path= null){
    return $path;
})->where('path', '(.*)');
Run Code Online (Sandbox Code Playgroud)