【问题标题】:Yii routing - how to put that controllerYii 路由 - 如何放置该控制器
【发布时间】:2015-07-03 21:03:58
【问题描述】:

我需要为以下 url 架构执行路由:

website.com/some-category-name
website.com/some-category-name/entryName

some-category-name 将是可变的 - 某个类别的名称

如何为此配置路由?我需要输入以前的控制器,例如:

website.com/account
website.com/regiter

并希望所有没有控制器名称(类别名称也是如此)的东西都进入控制器类别。

我做不出来。

【问题讨论】:

    标签: php yii routing


    【解决方案1】:

    使用

    'urlManager' => array(
            'urlFormat' => 'path',
            'showScriptName' => false, 
            'rules' => array(
                'categoryName/<categoryName:\w+>' => array('site/category'),
                'register' => array('site/register'),
                'account' => array('site/account')
            ),
        ),
    

    【讨论】:

      【解决方案2】:

      首先,您必须为“非 Category”操作声明所有规则,然后是动态规则(与 category 和 antry 相关联):

      'urlManager' => array(
          'urlFormat' => 'path',
          'showScriptName' => false, 
          'rules' => array(
              // for example if your account and register actions in user controller
              // ... you can write
              'account' => 'user/account',
              'register' => 'user/register',
              // or with one rule
              '<action(account|register)>' => 'user/<action>',
              // and for all other 'static actions', such as login, logout ...
      
              // after yhat you can declire dynamic rules
              '<categoryName:\w+>' => 'category/index',
              '<categoryName:\w+>/<entryName:\w+>' => 'category/entry'
          ),
      ),
      

      所以代码Yii::app()-&gt;createUrl('user/register') 将生成url website.com/register,相应地,url website.com/register "goes to" user 控制器的register 动作(所有其他静态像这样的规则)。

      现在动态规则:代码

      Yii::app()->createUrl('category/index', array(
          'categoryName' => 'first-category-name'
      )) 
      

      会生成url website.com/first-category-name,反之亦然:url website.com/first-category- name "goes to" category/index 动作,其中将有 $_GET['categoryName'] 参数,这将是相等的到“第二类名称”․

      相应代码

      Yii::app()->createUrl('category/index', array(
          'categoryName' => 'some-category-name',
          'entryName' => 'some-entry-name'
      ))
      

      会生成url website.com/some-category-name/some-entry-name,在category/entry动作中可以得到$_GET ['categoryName'] 等于 "some-category-name" 和 $_GET['entryName'] 等于 some-entry-name

      我希望这能帮助你理解 Yii 中的规则是如何工作的。

      谢谢!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-10-12
        • 2015-12-28
        • 1970-01-01
        • 2019-12-12
        • 1970-01-01
        • 2016-07-12
        • 2014-04-20
        • 1970-01-01
        相关资源
        最近更新 更多