How to change url parameter as directory in Yii2?

I am using yii2.I have a problem with the structure of the url. How can I change the URL structure in Yii2, my current url is below

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

My expected URL

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

I use the following link to change the default Yii2 URL

Yii2 htaccess - How to hide the / web interface and backend / web FULLY

+5
source share
2 answers

Web.php

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

If you have an alphabetical parameter , use.

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

For more information, click a URL that does not accept an alpha numeric parameter

+4
source
 '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

+2
source

Source: https://habr.com/ru/post/1238617/


All Articles