How to use the Yii2 class GroupUrlRule ()

I want to group paths under one common path. I found in the Yii2 documentation that this can be achieved with a class GroupUrlRule(). I can’t figure out where to install it. I tried to sit, as a rule, urlManagerin confing/web.php, but nothing happened.

+4
source share
2 answers

Imagine you have a 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 the URL hostname.com/module will be called 'module / controller / index'.

+5
source

You can do this in a file Bootstrap. Example:

Project /Bootstrap.php

namespace app;

use yii\base\BootstrapInterface;
use yii\web\GroupUrlRule;

class Bootstrap implements BootstrapInterface
{
    public $urlRules = [
        'prefix' => 'admin',
        'rules' => [
            'login' => 'user/login',
            'logout' => 'user/logout',
            'dashboard' => 'default/dashboard',
        ],
    ];

    public function bootstrap($app)
    {
        $app->get('urlManager')->rules[] = new GroupUrlRule($this->urlRules);
    }
}

Project / configurations / web.php

return [
    // ...
    'bootstrap' => [
        'log',
        'app\Bootstrap',
    ],
    // ...
]

P.S. Bootstrap . . Bootstrap . Bootstrap .

+4

Source: https://habr.com/ru/post/1545252/


All Articles