0

Пишу api модуль на yii2, и использую для версионирования субмодули. И проблема в том, что не могу получить доступ к странице в суб модуле, просто получаю 404. Вот так я описал его в config:

'modules' => [
    'api' => [
        'basePath' => '@app/api',
        'class' => app\api\ApiModule::class,
        'modules' => [
            'v1' => [
                'basePath' => '@app/api/v1',
                'class' => app\api\v1\V1Module::class,
            ]
        ],
        'components' => [
            'request' => [
                'class' => 'yii\web\Request',
                'parsers' => [
                    'application/json' => 'yii\web\JsonParser',
                ],
                'enableCsrfValidation' => false,
                'enableCsrfCookie' => false,
            ],
            'response' => [
                'class' => 'yii\web\Response',
                'formatters' => [
                    \yii\web\Response::FORMAT_JSON => [
                        'class' => 'yii\web\JsonResponseFormatter',
                        'prettyPrint' => YII_DEBUG,
                        'encodeOptions' => JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
                    ]
                ]
            ]
        ]
    ],
],
...
'urlManager' => [
        'enablePrettyUrl' => true,
        'enableStrictParsing' => true,
        'showScriptName' => false,
        'rules' => [
            ['class' => 'yii\rest\UrlRule', 'controller' => ['api/v1/user']],

            '/' => 'site/index',
        ],
    ],

Нужна ваша помощь.

1 ответ 1

0

Вообщем-то я так и не понял в чём проблема, но теперь всё работает, а настройка выглядит так:

web.php
'modules' => [
    'api' => [
        'basePath' => '@api',
        'class' => 'app\api\ApiModule',
        'modules' => [
            'v1' => [
                'basePath' => '@api/v1',
                'class' => 'app\api\v1\V1Module',
            ]
        ],
        'components' => $config_api
    ]
],
...
'urlManager' => [
    'enablePrettyUrl' => true,
    'enableStrictParsing' => true,
    'showScriptName' => false,
    'rules' => [
        ['class' => 'yii\rest\UrlRule', 'controller' => ['api/v1/user'],
            'patterns' => [
                'PUT,PATCH {id}' => 'update',
                'DELETE {id}' => 'delete',
                'GET,HEAD {id}' => 'view',
                'POST' => 'create',
                'GET,HEAD' => 'index',
                '{id}' => 'options',
                '' => 'options',
                'OPTIONS <id:\d+>/<action:[\w-]+>' => 'options',
                'OPTIONS <action:[\w-]+>' => 'options',
                '<id:\d+>/<action:[\w-]+>' => '<action>',
                '<action:[\w-]+>' => '<action>'
            ]
        ],

        '/' => 'site/index',
    ]
]

Ваш ответ

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

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