0

Знаю, тема довольно заезженная, но что-то у меня не получается

  1. Создал модуль со своим нэймспейсом:

frontend\config\main-local.php

'bootstrap' => ['debug', 'gii', 'user'],
    'modules' => [
        'gii' => [
            'class' => 'yii\gii\Module',
            'allowedIPs' => ['*']
        ],
        'debug' => [
            'class' => 'yii\debug\Module',
            'allowedIPs' => ['*'],
        ],
        'user' => [
            'class' => 'garmayev\user\Module',
        ],
    ],
    'aliases' => [
        '@garmayev/user' => '@app/modules/user'
    ],
  1. Пытаюсь сформировать правила роутинга в модуле:

@app\modules\user\Module.php

class Module extends \yii\base\Module implements BootstrapInterface
{
    public $controllerNamespace = 'garmayev\user\controllers';

    public function bootstrap($app)
    {
        $app->getUrlManager()->addRules([
            [
                'class' => 'yii\web\UrlRule',
                'pattern' => $this->id.'/list',
                'route' => $this->id.'/default/index'
            ], [
                'class' => 'yii\web\UrlRule',
                'pattern' => $this->id . '/<controller:[\w\-]+>/',
                'route' => $this->id . '/<controller>/index'
            ], [
                'class' => 'yii\web\UrlRule',
                'pattern' => $this->id . '/<controller:[\w\-]+>/<action:[\w\-]+>',
                'route' => $this->id . '/<controller>/<action>'
            ],


        ], false);
    }

    public function init()
    {
        parent::init();

        // custom initialization code goes here
        \Yii::configure($this, require(__DIR__ . '/config/main.php'));
    }
}
  1. UrlManager для остального сайта:

frontend\config\main.php

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => [
        '' => 'site/index',
        '<action:\w+>' => 'site/<action>',
        '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
    ],
],

Ошибки вроде бы очевидны:

При попытке открыть страницу user/list движок Yii2 (advanced) ищет контроллер user и экшн list (исходя из основных настроек в файле frontend\config\main.php) и соответственно вываливает ошибку 404

Вопрос: Как задать правила роутинга для модуля?

1 ответ 1

0

Кажется урлы надо в инит добавить. Я бы вынес все правила в отдельный конфиг-файл, чтобы не захламлять класс модуля. А так как в этому конфиге рулсы только для модуля, то id модуля можно и строкой прописать.

public function init()
{
    parent::init();
    Yii::$app->urlManager->addRules(require __DIR__ . '/config/urlRules.php');
}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.