Yii2 URL管理器不解析规则

时间:2015-07-21 15:18:24

标签: php yii2

我尝试使用Yii网址管理器将/site/about?tab=value等网址重写为site/about/valuesite/value

我的规则/<tab:\w+>' => 'site/about不起作用。以下是完整的URL管理器代码:

'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' =>
        [
            '/' => 'site/index',                
            '/deals' => '/deal/deals',
            '/<tab:\w+>' => 'site/about', //my rule
            '/about' => 'site/about',
            '/<action:[\-\w]+>' => 'site/<action>',
            '/<module:\w+>'  => '<module>/default/index',
            '/<module:\w+>/<controller:\w+>/<id:\d+>'  => '/<module>/<controller>/view',
            '/<module:\w+>/<controller:\w+>/<id:\d+>/<action:[\-\w]+>/<entity_id:\d+>'  => '/<module>/<controller>/<action>',
            '/<module:\w+>/<controller:\w+>/<id:\d+>/<action:[\-\w]+>'  => '/<module>/<controller>/<action>',
            '/<controller:\w+>/<id:\d+>'  => '<controller>/view',
            '/<controller:\w+>/<id:\d+>/<action:[\-\w]+>/<entity_id:\d+>'  => '<controller>/<action>',
            '/<controller:\w+>/<id:\d+>/<action:[\-\w]+>'  => '<controller>/<action>',

            '/<controller:\w+>/<action:[\-\w]+>'  => '<controller>/<action>',
            '/<module:\w+>/<action:[\-\w]+>'  => '<module>/default/<action>',
            '/<module:\w+>/<controller:\w+>'  => '<module>/<controller>/index',

        ],

这是小部件代码:

echo Nav::widget([
    'items' => [
        ['label' => '1', 'url' => ['/site/about', 'tab' => '']],
        ['label' => '2', 'url' => ['/site/about', 'tab' => 'value']],
        ['label' => '3', 'url' => ['site/about', 'tab' => 'tariffs']],
        ['label' => '4', 'url' => ['site/about', 'tab' => 'team']],
        ['label' => '5', 'url' => ['site/about', 'tab' => 'documents']],
    ],
    'options' => ['class' =>'nav-pills nav-stacked c-nav'],
]);

2 个答案:

答案 0 :(得分:1)

您应该使用命名参数,但是您正在命名模块/控制器部件 您只能使用这3个保留字:

  

模块,控制器,动作

docs示例类似:

[
    '<controller:(post|comment)>/<id:\d+>/<action:(create|update|delete)>' => '<controller>/<action>',
    '<controller:(post|comment)>/<id:\d+>' => '<controller>/view',
    '<controller:(post|comment)>s' => '<controller>/index',
]

但您使用<tab:\w+>并不代表规则部分的任何内容

可能你可以使用:

'/site/about/<tab:\w+>' => 'site/about',

捕获以下网址:

  

web.com/site/about/bla

然后在控制器中,您可以使用$tab变量,如

namespace app\controllers;

class SiteController extends \yii\web\Controller {
        public function actionAbout($tab)
        {
            return  $this->renderContent("Hello $tab");
        }
    }

希望这有帮助。

答案 1 :(得分:1)

问题在于,/<controller:\w+>/<action:[\-\w]+>' => '<controller>/<action>/<action:[\-\w]+>' => 'site/<action>之类的某些规则比我的解析早,所以我更改了它们。 这是我的解决方案:

'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' =>
        [
            '/' => 'site/index',
            '<tab:(about)>' => 'site/about',
            '/about/<tab:(about|value|documents|tariffs|team)>' => 'site/about',
            '/<action:(index|login|logout|contact|signup|request-password-reset|reset-password|thank-for-register|connect-inviteduser)>' => 'site/<action>',
            '/<controller:(deal)>/<action:\w>'  => 'deal/<action>',
            '/<module:(trades)>'  => '<module>/default/index',
            '/<module:(trades)>/<controller:\w+>/<id:\d+>'  => '/<module>/<controller>/view',
            '/<module:(trades)>/<controller:\w+>/<id:\d+>/<action:[\-\w]+>/<entity_id:\d+>'  => '/<module>/<controller>/<action>',
            '/<module:(trades)>/<controller:\w+>/<id:\d+>/<action:[\-\w]+>'  => '/<module>/<controller>/<action>',
            '/<controller:\w+>/<id:\d+>'  => '<controller>/view',
            '/<controller:\w+>/<id:\d+>/<action:[\-\w]+>/<entity_id:\d+>'  => '<controller>/<action>',
            '/<controller:\w+>/<id:\d+>/<action:[\-\w]+>'  => '<controller>/<action>',
            '/<module:(trades)>/<action:[\-\w]+>'  => '<module>/default/<action>',
            '/<module:(trades)>/<controller:\w+>'  => '<module>/<controller>/index',

        ],
    ],

小部件代码:

<?php
                        echo Nav::widget([
                            'items' => [
                                ['label' => 'О проекте', 'url' => ['/site/about', 'tab' => 'about']],
                                ['label' => '5 причин работать через портал', 'url' => ['/site/about', 'tab' => 'value']],
                                ['label' => 'Продвижение и тарификация', 'url' => ['site/about', 'tab' => 'tariffs']],
                                ['label' => 'Проектная команда', 'url' => ['site/about', 'tab' => 'team']],
                                ['label' => 'Документы', 'url' => ['site/about', 'tab' => 'documents']],
                            ],
                            'options' => ['class' =>'nav-pills nav-stacked c-nav'],
                        ]);
                        ?>

动作代码:

public function actionAbout($tab = 'about')
{
    $this->layout = '/about';

    $tab = $tab == 'about' ? 'index' : $tab;
    if (Yii::$app->request->isAjax)
        return $this->renderPartial('about/' . $tab);
    else
        return $this->render('about/' .$tab);
}