Search code examples
phpyii2yii-routing

How to use the Yii2 GroupUrlRule() class


I want to group paths under one common path. I found in the Yii2 documentation that this can be achieved with the GroupUrlRule() class. I can not understand where to set it. I tried to sat it as a rule to the urlManager in confing/web.php but nothing happened.


Solution

  • Imagine that you have some module. Your confing/web.php file might look like this:

    'components' => [
        'urlManager' => [
            'class' => 'yii\web\UrlManager',
            'showScriptName' => false,
            'enablePrettyUrl' => true,
            'rules' => [
                [
                    'class' => 'yii\web\GroupUrlRule',
                    'prefix' => 'module',
                    'rules' => [
                        '/' => 'controller/index',
                        'create' => 'controller/create',
                        'edit' => 'controller/edit',
                        'delete' => 'controller/delete',
                    ],
                ],
            ],
        ],
    ]
    

    Now, by URL hostname.com/module will be called 'module/controller/index'.