我正在学习模块如何在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
],
];
您需要以这种方式配置控制器:
'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