Alejandro
Alejandro

Reputation: 866

Yii2 - Remove default module controller from URL

I have the following rules in my urlManager:

<?php

    $uuidV4Pattern = '[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}';
    
    $config = [
        ...
        'components' => [
            'urlManager' => [
                'enablePrettyUrl' => true,
                'showScriptName' => false,
                'rules' => [
                    '/' => 'site/index',
                    '<action:[a-zA-Z_]+>' => 'site/<action>',

                    '<controller:\w+>/<id:' . $uuidV4Pattern. '>'=>'<controller>/view',
                    '<controller:\w+>/<id:\d+>'=>'<controller>/view',
                    '<controller:\w+>/<action:[a-zA-Z_]+>/<id:' . $uuidV4Pattern. '>'=>'<controller>/<action>',
                    '<controller:\w+>/<action:[a-zA-Z_]+>/<id:\d+>'=>'<controller>/<action>',
                    '<controller:\w+>/<action:[a-zA-Z_]+>'=>'<controller>/<action>',

                    '<module:\w+>/<controller:\w+>/<id:' . $uuidV4Pattern. '>'=>'<module>/<controller>/view',
                    '<module:\w+>/<controller:\w+>/<id:\d+>'=>'<module>/<controller>/view',
                    '<module:\w+>/<controller:\w+>/<action:[a-zA-Z_]+>/<id:' . $uuidV4Pattern. '>'=>'<module>/<controller>/<action>',
                    '<module:\w+>/<controller:\w+>/<action:[a-zA-Z_]+>/<id:\d+>'=>'<module>/<controller>/<action>',
                    '<module:\w+>/<controller:\w+>/<action:[a-zA-Z_]+>'=>'<module>/<controller>/<action>',

                ]
            ]
        ],
        ...
    ];

But I would like that when accessing an action of my modules do it in the following way:

https://example.com/mymodule
https://example.com/mymodule/action
https://example.com/mymodule/controller/action

instead of:

https://example.com/mymodule/default/index
https://example.com/mymodule/default/action
https://example.com/mymodule/controller/action

I inserted these two rules as follows:

'rules' => [
    '/' => 'site/index',
    '<action:[a-zA-Z_]+>' => 'site/<action>',

    '<controller:\w+>/<id:' . $uuidV4Pattern. '>'=>'<controller>/view',
    '<controller:\w+>/<id:\d+>'=>'<controller>/view',
    '<controller:\w+>/<action:[a-zA-Z_]+>/<id:' . $uuidV4Pattern. '>'=>'<controller>/<action>',
    '<controller:\w+>/<action:[a-zA-Z_]+>/<id:\d+>'=>'<controller>/<action>',
    '<controller:\w+>/<action:[a-zA-Z_]+>'=>'<controller>/<action>',
    
    // New rules
    '<module:\w+>/<action:\w+>/<id:(.*?)>' => '<module>/default/<action>/<id>',
    '<module:\w+>/<action:\w+>' => '<module>/default/<action>',
    
    '<module:\w+>/<controller:\w+>/<id:' . $uuidV4Pattern. '>'=>'<module>/<controller>/view',
    '<module:\w+>/<controller:\w+>/<id:\d+>'=>'<module>/<controller>/view',
    '<module:\w+>/<controller:\w+>/<action:[a-zA-Z_]+>/<id:' . $uuidV4Pattern. '>'=>'<module>/<controller>/<action>',
    '<module:\w+>/<controller:\w+>/<action:[a-zA-Z_]+>/<id:\d+>'=>'<module>/<controller>/<action>',
    '<module:\w+>/<controller:\w+>/<action:[a-zA-Z_]+>'=>'<module>/<controller>/<action>',

],

But not working. I do not know what I am missing, I would appreciate they gave me some light.

Upvotes: 0

Views: 213

Answers (1)

Alejandro
Alejandro

Reputation: 866

Ok, the way i managed (in a simple way) to solve this was to edit my rules as follows:

'rules' => [    
        '<module:\w+>/<action:(dashboard)>' => '<module>/default/<action>',
        
        '<controller:\w+>/<action:[a-zA-Z_]+>/<id:' . $uuidV4Pattern. '>'=>'<controller>/<action>',
        '<controller:\w+>/<action:[a-zA-Z_]+>/<id:\d+>'=>'<controller>/<action>',
        '<controller:\w+>/<action:[a-zA-Z_]+>'=>'<controller>/<action>',
        
        '<module:\w+>/<controller:\w+>/<action:[a-zA-Z_]+>/<id:' . $uuidV4Pattern. '>' => '<module>/<controller>/<action>',
        '<module:\w+>/<controller:\w+>/<action:[a-zA-Z_]+>/<id:\d+>' => '<module>/<controller>/<action>',
        '<module:\w+>/<controller:\w+>/<action:[a-zA-Z_]+>' => '<module>/<controller>/<action>',

]

I had to force the name of the actions as it seems (I'm not sure) that they conflict with the names of the other controllers in the module.

I am not satisfied as I would have wanted to solve everything with patterns and regular expressions.

Note: I don't mark it as the correct answer hoping someone else can come up with a more "elegant" solution.

Upvotes: 1

Related Questions