phpyii2yii2-urlmanager

Yii2 urlmanager rules for pretty urls fails


I have the following url manager path

        'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => [
            '<controller:(\w|-)+>/' => 'site/index',
            '<module:api\w+>/<controller:\w+>/<action:(\w|-)+>' => '<module>/<controller>/<action>',
         ],
    ]

What am looking for is all urls not rendered via api module paths to run via site/index but all other paths having api/* to be executed via module paths.

The above works for urls like /login, /auth but when i run urls like

/administrative/uom 

It fails

SO basically i want all urls to be redirrected via site/index but all urls having api as the prefix like api/auth/login to be run via their respective controllers. I have added an api module which should handle this.

What else do i need to add to make this work?


Solution

  • When adding rules always start from more detailed to less detailed. And you added general rule for controller only so no URL with action is matched. Do this

    'rules' => [
        'api/<controller:\w+>/<action:[\w\-]+>' => 'api/<controller>/<action>',
        '<controller:[\w\-]+>/<action:[\w\-]+>' => 'site/index',
        '<controller:[\w\-]+>/' => 'site/index',
    ],