Yii2 REST API not sending POST to actionCreate?

Hi Folks,

I’m probably missing something, but I’ve created a test controller, and implemented actionIndex, actionView, actionUpdate, and actionCreate.

A GET /v2/contacts goes to actionIndex, GET /v2/contacts/{id} goes to actionView($id), PUT /v2/contacts/{id} goes to actionUpdate($id), however a POST to /v2/contacts ends up going to actionIndex, and is then blocked as POST isn’t an allowed verb on actionIndex.

I have created a modules/v2/controllers/APIController which extends yii\rest\Controller, and does:

protect function verbs() {

return [

   'index' => ['GET', 'HEAD'],


   'view' => ['GET', 'HEAD'],


   'create' => ['POST'],


   'update' => ['PUT', 'PATCH'],


   'delete' => ['DELETE'],

];

}

And then I have a ContactsController which extends it, and implements the various action* functions.

In my config/main.php for api, I have:

    'urlManager' => [


       'cache' => false,


       'rules' => [


         [


           'class'=> 'yii\rest\UrlRule',


           'controller' => [


             'v2/contacts',


           ],


         ],


       ],


     ],

Have I missed something? As I mentioned, everything seems to work EXCEPT the POST. I’ve put 6 hour or so into tracing through the code and so far I’m pulling up blanks.

Thanks,

Damien

Answering my own question. We had a couple of default rules in our common/config/main.php - I didn’t realise these were processed before the application rules - and so <controller>/<action> was catching the v2/contacts instead of the POST rule (and the GET,HEAD rule for that matter, it was just still working as it was still going to the same place).

Split it out to identical configs in all applications rather than one definition in common, and it’s working nicely now :)

Can you tell me more detail?

[color="#006400"]/* Moved from "General Discussions" to "REST APIs" */[/color]