Answer the question
In order to leave comments, you need to log in
How to setup yii2 rest routing?
Hello. I'm reading the yii2 RESTful Routing Guide , but it doesn't work out in practice. I'm trying, depending on the verb (HTTP Verb), to work out the corresponding device controller action, which is in the api module and is inherited from yii\rest\Controller. My urlManager looks like this
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => false,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => 'api\device',
],
//'GET api/device' => 'api/device/index',
//'POST api/device' => 'api/device/create',
'<module:[\w-]+>/<controller:[\w-]+>/<action:[\w-]+>/<id:\d+>' => '<module>/<controller>/<action>',
'api/search-dev/<serilal:[\w-]+>' => 'api/search-dev',
'api/dev-models/type/<type_id:\d+>/brand/<brand_id:\d+>' => 'api/dev-models',
],
],
namespace app\modules\api\controllers;
use yii\rest\Controller;
class DeviceController extends Controller {
public function actionIndex() {
$resp = new \stdClass();
$resp->action = 'index';
return $resp;
}
public function actionCreate() {
$resp = new \stdClass();
$resp->action = 'create';
return $resp;
}
}
Answer the question
In order to leave comments, you need to log in
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
'GET api/device' => 'api/device/index',
'POST api/device' => 'api/device/create',
'<module:[\w-]+>/<controller:[\w-]+>/<action:[\w-]+>/<id:\d+>' => '<module>/<controller>/<action>',
'GET api/search-dev/<serilal:[\w-]+>' => 'api/search-dev',
'GET api/dev-models/type/<type_id:\d+>/brand/<brand_id:\d+>' => 'api/dev-models',
'<module:[\w-]+>/<controller:[\w-]+>' => '<module>/<controller>',
],
],
your first rule processes everything, in the same guide that you use there is an example:
[
'class' => 'yii\rest\UrlRule',
'controller' => 'user',
'extraPatterns' => [
'GET search' => 'search',
],
]
[
'class' => 'yii\rest\UrlRule',
'controller' => 'api\device',
'extraPatterns' => [
'POST create' => 'create',
],
]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question