Yii - Hiding module name in URL

2020-02-07 06:14发布

My present URL structure is as below:

http://www.mydomain.com/module/controller/action

I need to hide the module section of the URL. Is there any way this can be done?

Thanks.

标签: module yii
2条回答
Lonely孤独者°
2楼-- · 2020-02-07 06:34

To point the URL http://www.mydomain.com/customer/login to the module, in you config (protected/config/main.php) under urlManager:

'urlManager'=>array(
        'urlFormat'=>'path',
        'showScriptName' => false,
        'rules'=>array(
            'customer/login' => 'module/controller/action',

            '<controller:\w+>/<id:\d+>'=>'<controller>/view',
            '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
            '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
        ),
    ),

To make any controller action go to module/controller/action (as discussed below) you can use:

'<controller:\w+>/<action:\w+>'=>'module/controller/action',

or

'<controller:\w+>/<action:\w+>'=>'module/<controller:\w+>/<action:\w+>',

Depending on whether the controller/action part of the value (on the right hand side of the =>) is a set value, or a variable.

So if you want any controller/action to go to the exact url module/controller/action, you would use the first example. For example if you want the controller/action site/test to go to module/controller/action, you would use the first example above

If you want any controller/action to go to a dynamic controller/action you use the second. For example, if you want the controller/action site/test to go to module/site/test, you would use the second example above

This new rule must be above the 3 default Yii rules as they are read and top to bottom and match the first rule it finds only

查看更多
时光不老,我们不散
3楼-- · 2020-02-07 06:57

Yes, you can define any url rules in your config.

Your url rule may look something like this:

'<controller:(foo|bar)>/<action>' => 'module/<controller>/<action>',
查看更多
登录 后发表回答