Url Manager在YII中隐藏控制器名称和功能名称

时间:2016-11-26 08:37:02

标签: php yii yii2

我是YII框架的新手。我必须隐藏默认的控制器名称和功能名称。 例如:

(现有网址:http://localhost/food/store/home

(必填网址:http://localhost/food/

在YII框架配置页面中,他们已将urlManager声明为:

配置页面中的URL管理:

'urlManager'=>array(
    'urlFormat'=>'path',
    'showScriptName' => false,
    'rules'=>array(           
        '<controller:\w+>/<id:\d+>'=>'<controller>/view',
        '<controller:\w+>'=>'<controller>/index',
        '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
        '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
    ),
    'showScriptName'=>false,
)

默认控制器声明:

'defaultController'=>'store'

2 个答案:

答案 0 :(得分:0)

您可以在不使用<controller><action>等占位符的情况下创建特定规则,只需确保在一般情况的规则之前添加它们。

'urlManager' => [
    'urlFormat' => 'path',
    'showScriptName' => false,
    'rules' => [
        //Add the rules for the specific cases
        '' => 'store/home',

        // The general case rules go after the specific cases
        '<controller:\w+>/<id:\d+>' => '<controller>/view',
        '<controller:\w+>' => '<controller>/index',
        '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
        '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
    ],
],

答案 1 :(得分:0)

使用别名会解决问题  'urlManager' => array( 'urlFormat' => 'path', 'rules' => array( 'admin' => '/admin/default', 'mobile' => '/mobile/default', 'mobile/<alias:fees|aboutus|contactus|terms|policy|faq|aml|legal|news|testimonial>' => 'mobile/default/<alias>', '<alias:fees|about|contactus|terms|privacypolicy|faq|aml|legal>' => 'site/<alias>', '<module:\w+>/<controller:\w+>/<action:\w+>' => '<module>/<controller>/<action>', '<controller:\w+>/<id:\d+>' => '<controller>/view', '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>', '<controller:\w+>/<action:\w+>' => '<controller>/<action>', ), 'showScriptName' => false, ),