Laravel 5.1 Wildcard Route

对着背影说爱祢 提交于 2019-12-21 15:15:24

问题


I'm creating a CMS that allows the user to define categories. Categories can either have additional categories under it or pages. How can I create a route in Laravel that will support a potentially unlimited number of URI segments?

I've tried the following....

Route::get('/resources/{section}', ['as' => 'show', 'uses' => 'MasterController@show']);

I also tried making the route optional...

Route::get('/resources/{section?}', ['as' => 'show', 'uses' => 'MasterController@show']);

Keep in mind, section could be multiple sections or a page.


回答1:


First, you need to provide a regular expression to be used to match parameter values. Laravel router treats / as parameter separator and you must change that behaviour. You can do it like that:

Route::get('/resources/{section}', 
  [
    'as' => 'show', 
    'uses' => 'MasterController@show'
  ])
  ->where(['section' => '.*']);

This way, whatever comes after /resources/ and matches the regular expression will be passed to $section variable in your controller.



来源:https://stackoverflow.com/questions/31569828/laravel-5-1-wildcard-route

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