Laravel 5.1通配符路由 [英] Laravel 5.1 Wildcard Route

查看:423
本文介绍了Laravel 5.1通配符路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个CMS,允许用户定义类别.类别可以在其下或页面下具有其他类别.如何在Laravel中创建一条路由,该路由将支持无限数量的URI段?

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.

推荐答案

首先,您需要提供一个用于匹配参数值的正则表达式. Laravel路由器将/视为参数分隔符,您必须更改其行为.您可以这样做:

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' => '.*']);

通过这种方式,在/resources/之后且与正则表达式匹配的所有内容都将传递到控制器中的 $ section 变量中.

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

这篇关于Laravel 5.1通配符路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆