Using a regex to match a substring in a Laravel route

爷,独闯天下 提交于 2019-12-05 14:31:14

I got it working with ^([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\-]+)?

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.

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