restyii2http-verbs

Why I don't have to specify verbs() for "GET" for my custom REST action?


I have created a standard yii\rest\ActiveController and added my custom action to it:

public function actionTest($id)
{
    return ['test' => $id];
}

I have added a new entry to extraPatterns of rules of yii\web\UrlManager:

'extraPatterns' => [
    'GET test/{id}' => 'test',
],

Following this answer, I have also added new verb definition in my REST controller:

public function verbs()
{
    $verbs = parent::verbs();
    $verbs['test'] = ['GET'];
    return $verbs;
}

But it turned out to be not needed. Meaning that I can comment out the above piece of code (the entire method in this case) and the whole new REST route is still working:

enter image description here

enter image description here

What am I missing? Why I don't need to specify this new action / router in verbs() for GET (I figured out that I must do this for other verbs)?


Solution

  • In the referenced question/answer you were working with existing action index that already had rule for yii\filters\VerbFilter to only allow GET or HEAD verbs for that action.

    But in case of completely new action test there is no existing rule for VerbFilter. If there is no rule for action the VerbFilter will allow any request. That's why if you don't specify rule for test action any verb set up in router will work. Once you specify the rule for test in verbs() method, only allowed verbs will make it through VerbFilter.