Yii2高级版-在模块中启用漂亮的URL

时间:2019-02-15 08:58:24

标签: yii2 yii2-advanced-app yii-routing yii-rest

我正在学习模块如何在Yii2中工作,现在我创建了以下模块:gdpr。我可以访问以下路线:/index.php?r=gdpr/user/index。但是,我要访问这样的路由:/gdpr/user/index。我该如何实现?

config.php:

<?php
return [
    'components' => [
        'urlManager' => [
            'class' => 'yii\web\UrlManager',
            'enablePrettyUrl' => true,
            'enableStrictParsing' => true,
            'showScriptName' => false,
            'rules' => [
                ['class' => 'yii\rest\UrlRule', 'controller' => 'modules\gdpr\default'],
                ['class' => 'yii\rest\UrlRule', 'controller' => 'modules\gdpr\user'],
            ],
        ],
    ],
    'params' => [
        // list of parameters
    ],
];

1 个答案:

答案 0 :(得分:0)

您需要通过以下方式配置控制器:

'components' => [
    'urlManager' => [
        'class' => 'yii\web\UrlManager',
        'enablePrettyUrl' => true,
        'enableStrictParsing' => true,
        'showScriptName' => false,
        'rules' => [
            [
                'class' => 'yii\rest\UrlRule', 
                'controller' => ['gdpr/default', 'gdpr/user'],
            ],
        ],
    ],
],

请参见https://www.yiiframework.com/doc/api/2.0/yii-rest-urlrule#$controller-detail