Yii2 advanced - enable pretty URL in module

1.1k Views Asked by At

I am learning how modules work in Yii2 and now I created the following module: gdpr. I can access the following route: /index.php?r=gdpr/user/index. However, I want to access the route like this: /gdpr/user/index. How can I achieve that?

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

There are 1 best solutions below

0
On

You need to configure controllers in this way:

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

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