[英]Using a regex to match a substring in a Laravel route
My URL is: www.foo.com/some-bar-slug-here/page 我的网址是:www.foo.com/some-bar-slug-here/page
I can't get a route to catch if the string "bar" is found in the slug shown above: 如果在上面显示的slug中找到字符串“bar”,我无法获取捕获的路径:
Route::any('{myslug}/page/', array('as'=>'bar-page', 'uses'=>'Controllers\MyBar@index'))
->where('myslug','/bar/');
If I use the regex expression [0-9A-Za-z\\-]+
it works, but it doesn't work for /bar/
. 如果我使用正则表达式
[0-9A-Za-z\\-]+
它可以工作,但它不适用于/bar/
。 Any ideas? 有任何想法吗?
I got it working with ^([0-9A-Za-z\\-]+)?bar([0-9A-Za-z\\-]+)?
我得到它与
^([0-9A-Za-z\\-]+)?bar([0-9A-Za-z\\-]+)?
So the updated route code looks like this: 所以更新的路由代码如下所示:
Route::any('{myslug}/page/', array('as'=>'bar-page', 'uses'=>'Controllers\MyBar@index'))
->where('myslug','^([0-9A-Za-z\-]+)?bar([0-9A-Za-z\-]+)?');
Bonus: To make it case insensitive, I do this: ^([0-9A-Za-z\\-]+)?(?i)bar([0-9A-Za-z\\-]+)?
额外:为了使它不区分大小写,我这样做:
^([0-9A-Za-z\\-]+)?(?i)bar([0-9A-Za-z\\-]+)?
Note: If you are using a copy of this route further down in the your routes file, but searching for a different sub-string then you will need to name your {myslug}
to something different like {myslug2}
, otherwise Laravel will not run all of the routes. 注:如果您使用这条路线在你的路由文件进一步下跌的副本,而是寻找一个不同的子串,那么你将需要命名
{myslug}
喜欢的东西不同{myslug2}
否则Laravel将无法运行所有的路线。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.