Answer the question
In order to leave comments, you need to log in
Yii2 rest api 404 error, what did you do wrong?
Good day! I did everything as per the instructions, but I feel that I didn’t write something 100% correctly, since the instruction was made simpler, but for me it’s a bit not simple.
I use an advanced template, there is such a path: backend/modules/media_library/сontrollers/rest/
In this folder, lie MediaLibraryController.php
<?php
namespace backend\modules\media_library\controllers\rest;
use yii\rest\ActiveController;
class MediaLibraryController extends ActiveController
{
public $modelClass = 'backend\modules\media_library\models\MediaLibrary';
}
'components' => [
'request' => [
'csrfParam' => '_csrf-backend',
'baseUrl' => '/admin',
'parsers' => [
'application/json' => 'yii\web\JsonParser',
],
'cookieValidationKey' => 'key',
],
...
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'' => 'site/index',
'<controller:\w+>/<action:\w+>/' => '<controller>/<action>',
['class' => 'yii\rest\UrlRule', 'controller' => 'media_library'],
],
]
],
Answer the question
In order to leave comments, you need to log in
The error was in the line where I specified the controller, it would, of course, be specified incorrectly.
Corrected for
['class' => 'yii\rest\UrlRule', 'controller' => 'media-library/rest/media-library']
and everything works.
Did you add the module to the config?
'components' => [
//...
],
'modules' => [
'media_librarys' => [
'class' => backend\modules\media_library\Module::class
]
]
Для таких же ищущих ответ на этот вопрос поясню:
1. UrlRule ждет пропертю controller, которая в доке описана как ID контроллера, но это не совсем так. Если используете Advanced template, то надо указать и имя модуля moduleName/controller (user/auth).
2. Если вы также, как и я используете единственное число в именовании, то не забудьте выключить нафиг pluralize.
Тогда конечный вариант для меня выглядел так
['class' => 'yii\rest\UrlRule', 'controller' => 'user/auth', 'pluralize' => false],
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question