Yii - Hiding module name in URL

后端 未结 2 1871
闹比i
闹比i 2020-12-12 04:47

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 ca

相关标签:
2条回答
  • 2020-12-12 05:47

    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

    0 讨论(0)
  • 2020-12-12 05:50

    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>',
    
    0 讨论(0)
提交回复
热议问题