在yii2中重写url

时间:2015-12-01 03:55:30

标签: rewrite yii2

如何在Yii2中重写网址。我想重写网址

  

/后/索引?ID = 1

  

/后/ 1-示例

3 个答案:

答案 0 :(得分:1)

您需要在rules(如果是高级模板)或urlManager中为config/main.php添加以下web.php代码。

[
    'components' => [
        'urlManager' => [
            'showScriptName' => false,
            'enablePrettyUrl' => true,
            'enableStrictParsing' => false,
            'rules' => [
                   'post/<id:\d+>-example' => 'post/index',
            ],
        ],
    ],
]

答案 1 :(得分:0)

[
    'components' => [
        'urlManager' => [
            'showScriptName' => false,
            'enablePrettyUrl' => true,
            'enableStrictParsing' => false,
            'rules' => [
                   'post/<id:\d+>-<title:\w+>' => 'post/index',
            ],
        ],
    ],
]

答案 2 :(得分:0)

您需要在代码部分下的backend/config/main.php添加以下代码:

'urlManager' => [
    'class' => 'yii\web\UrlManager',
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => array(
        '<controller:\w+>/<id:\d+>' => '<controller>/view',
        '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
        '<controller:\w+>/<action:\w+>' => '<controller>/<action>',),
],

我发布的仅仅是因为ians应该是所有整个项目的通用。