A
A
Anton Artyomov2016-02-29 21:46:41
Yii
Anton Artyomov, 2016-02-29 21:46:41

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',
            ],
        ],

The controller looks like this
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;
    }
}

Regardless of the transmitted verb (POST or GET), I always work out the actionIndex device of the controller.
In the urlManager code, you can see that I tried to write rules like 'POST api/device' => 'api/device/create'. Unsuccessfully.
Does anyone understand how this works? Can you explain "popular"? Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Artyomov, 2016-03-29
@ArtyomovAnton

'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>',
            ],
        ],

Works with 'enableStrictParsing' => true without extraPatterns. Thanks for the answer.

E
enchikiben, 2016-03-05
@EnChikiben

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',
    ],
]

in your case it should be something like:
[
    'class' => 'yii\rest\UrlRule',
    'controller' => 'api\device',
    'extraPatterns' => [
        'POST create' => 'create',
    ],
]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question