在Yii 2的UrlManager规则中使用RegEx,不起作用

时间:2018-01-31 06:49:40

标签: php yii yii2 yii2-urlmanager

Yii2 urlManager,我想编写一个如下所示的正则表达式。

*.*com/bj/haidian/很好。

*.*com/bj/haidian不起作用。

'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => [
            '<module:\w+>/<controller:\w+>/<action:\w+>' => '<module>/<controller>/<action>',
            "<controller:\w+>/<action:\w+>/<id:\d+>" => "<controller>/<action>",
            "<controller:\w+>/<action:\w+>" => "<controller>/<action>",
            '<module:\w+>/<action:\w+>/<id:\d+>' => '<module>/default/<action>',
            '<module:\w+>/<controller:\w+>' => '<module>/<controller>/index',
            'bj|sh|gz|sz|cd|xa|wh|hz|tj|dl/?'=>'/sem/weight',
            'bj|sh|gz|sz|cd|xa|wh|hz|tj|dl/<district:[0-9a-zA-Z\_\-]+>/?' => '/sem/weight',
            'bj|sh|gz|sz|cd|xa|wh|hz|tj|dl/<district:[0-9a-zA-Z\_]+>/<cond:[0-9a-zA-Z\_]+>/?' => '/sem/weight',
        ],
    ],

1 个答案:

答案 0 :(得分:0)

您的规则应该在default规则或parameterized路由之前,不在它们之后,否则您的规则在我身边正常工作我只测试了所有3个规则,只需将序列更改为以下内容。 / p>

'urlManager' => [
        'class' => 'yii\web\UrlManager',
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => [
            'bj|sh|gz|sz|cd|xa|wh|hz|tj|dl/?'=>'/sem/weight',
            'bj|sh|gz|sz|cd|xa|wh|hz|tj|dl/<district:[0-9a-zA-Z\_\-]+>/?' => '/sem/weight',
            'bj|sh|gz|sz|cd|xa|wh|hz|tj|dl/<district:[0-9a-zA-Z\_]+>/<cond:[0-9a-zA-Z\_]+>/?' => '/sem/weight',
            '<module:\w+>/<controller:\w+>/<action:\w+>' => '<module>/<controller>/<action>',
            "<controller:\w+>/<action:\w+>/<id:\d+>" => "<controller>/<action>",
            "<controller:\w+>/<action:\w+>" => "<controller>/<action>",
            '<module:\w+>/<action:\w+>/<id:\d+>' => '<module>/default/<action>',
            '<module:\w+>/<controller:\w+>' => '<module>/<controller>/index',

        ],
    ],