Yii2 URL format

Hi friends,

I have a project using yii2 advanced and I want to change url from

http://127.0.0.1:8080/user/view?id=156

to

http://127.0.0.1:8080/user/156/view

or

http://127.0.0.1:8080/user/view/156

Kindly assist me please

If you have modules in your Yii2 Advanced project and you want to apply the URL format change to them as well, you can modify the URL rules to include the module ID. Here’s how you can achieve it:

  1. Open the common/config/main.php file.
  2. Locate the components array and find the urlManager configuration.
  3. Update the rules configuration to include modules. For example:
'components' => [
    'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => [
            '<module:\w+>/<controller:\w+>/<id:\d+>/<action:\w+>' => '<module>/<controller>/<action>',
            '<controller:\w+>/<id:\d+>/<action:\w+>' => '<controller>/<action>',
        ],
    ],
    // other components...
],
  1. Save the changes to the configuration file.

With this updated configuration, Yii2 will now map URLs of the form /module/controller/156/view to the action view in the controller of the specified module, passing the id parameter as 156.

Make sure to replace 'module' and 'controller' with the appropriate module and controller names, respectively, based on your project’s structure.

By following these steps, you should be able to apply the URL format change to modules in your Yii2 Advanced project as well.