Laravel可选路线参数

编程入门 行业动态 更新时间:2024-10-26 14:38:26
本文介绍了Laravel可选路线参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 Route::get('dashboard/{path?}', function($path= null) { return $path; });

是的,是的

如果网址是

仪表板/电影/搞笑/../..

获取 NotFoundHttpException

推荐答案

默认情况下为路由参数不能包含任何斜杠,因为多个路由参数或线段之​​间用斜杠分隔。

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)

但是,这不是很优雅,也不动态,您的示例表明可能存在很多路径级别。您可以使用where约束在route参数中使用斜线,因此该路由基本上可以捕获以 dashboard 开头的所有内容p>

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

更多推荐

Laravel可选路线参数

本文发布于:2023-07-14 22:14:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1107320.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可选   路线   参数   Laravel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!