如何在Yii2中更改目录中的url参数?

时间:2015-12-18 10:07:43

标签: php url yii2

我正在使用yii2.I有与url结构相关的问题。如何在Yii2中更改URL结构我的当前URL在下面给出

http://localhost/advanced/posts/view?id=1

我的预期网址是

http://localhost/advanced/posts/view/id/1

我已按照以下链接更改Yii2的默认网址

Yii2 htaccess - How to hide frontend/web and backend/web COMPLETELY

2 个答案:

答案 0 :(得分:3)

<强> Web.php

'urlManager' => [
      'showScriptName' => false,
      'enablePrettyUrl' => true,
        'enableStrictParsing' => false,
        'rules' => [
            '<controller>/<action>/<id:d+>' => '<controller>/<action>'
        ],
    ], 

如果有字母数字参数,请使用。

'urlManager' => [
          'showScriptName' => false,
          'enablePrettyUrl' => true,
            'enableStrictParsing' => false,
            'rules' => [
                '<controller>/<action>/<id:w+>' => '<controller>/<action>'
            ],
        ], 

如需更多信息,请点击URL Not Accepting Alpha Numeric Paramater

答案 1 :(得分:1)

'components' => [
    'urlManager' => [               
        'showScriptName' => false,  // Disable index.php
        'enablePrettyUrl' => true, // Disable r= routes
        'enableStrictParsing' => true,
        'rules' => array(
                'mycategory/<controller:\w+>/<action:\w+>' => '<controller>/<action>',
                '<controller:\w+>/<id:\d+>' => '<controller>/view',
                '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
                '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
                //Rules with Server Names
                'http://admin.domain.com/login' => 'admin/user/login',
                'http://www.domain.com/login' => 'site/login',
                'http://<country:\w+>.domain.com/profile' => 'user/view',
                '<controller:\w+>/<id:\d+>-<slug:[A-Za-z0-9 -_.]+>' => '<controller>/view',
            ),
    ],
],

并点击此链接: first link second link