Laravel Optional Route Parameters

蹲街弑〆低调 提交于 2019-12-12 01:59:49

问题


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

yeah that makes sense

what if url is

dashboard/movies/funny/../..

got NotFoundHttpException


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


来源:https://stackoverflow.com/questions/27343730/laravel-optional-route-parameters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!