Yii2 advanced - 在模块中启用漂亮的URL

问题描述 投票:0回答:1

我正在学习模块如何在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
    ],
];
yii2 yii2-advanced-app yii-routing yii-rest
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.