简体   繁体   中英

How to change url parameter like directory in Yii2?

I am using yii2.I have problem related to url structure. How I can change URL structure in Yii2 my current URL is given below

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

My expected URL is

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

I have follow the following link to change default URL of Yii2

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

Web.php

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

If Having alpha numeric parameter , then use.

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

For More Info, click URL Not Accepting Alpha Numeric Paramater

'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',
            ),
    ],
],

and follow this link : first link second link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM