【问题标题】:Yii2 - simplify multiple similar url rulesYii2 - 简化多个相似的 url 规则
【发布时间】:2018-08-24 22:48:51
【问题描述】:

我的 SiteController 中有很多操作。我不想在 URL 中出现site,所以我的配置文件中有这个:

'rules' => [
    '/' => 'site/index',
    'about' => 'site/about',
    'contact' => 'site/contact',
    'login' => 'site/login',
    'logout' => 'site/logout',
    'register' => 'site/register',
],

有没有办法将所有这些(不包括/ 路由)简化为一条规则?

【问题讨论】:

    标签: yii2


    【解决方案1】:

    你可以使用

    'rules' => [
        '<action:\w+>' => 'site/<action>',
    ],
    

    【讨论】:

      【解决方案2】:

      这是最好的方法:

      '<action:(about|contact|login|logout|register)>' => 'site/<action>',
      

      Shringiraj Dewangan 发布的答案将匹配任何请求并路由到站点控制器,而这只会匹配指定的请求。

      【讨论】:

        猜你喜欢
        • 2014-12-08
        • 2017-04-17
        • 1970-01-01
        • 1970-01-01
        • 2016-07-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多