phpyii2yii-url-manager

How to hide module name from url yii2


I am new to yii2.

mydomain.com/organisation/banks/index

from above url i want to remove organisation.

i add following code to my config file under component section.

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

But still its not working.

Is there any way to do it?

Thanks


Solution

  • In case of multiple URLs to hide I'm afraid you have to add the rule for each URL like:

    'banks/index' => 'organisation/banks/index',
    'something/another' => 'organisation/something/another',
    

    If there is an unique controller's name that this module uses you can make it easier by adding general rule. For example if controller's name is banks and there is no BanksController under the top namespace level of app you can add:

    'banks/<action>' => 'organisation/banks/<action>',
    

    If organisation module is the only one your app uses and you don't need top namespace level controllers you can add:

    '' => 'organisation',
    

    So entering the domain URL address gets you directly to organisation module default route.