REST API Implementation

Hello guys,

I’m facing some problems trying to implement a REST API in our project.
The problem boils down to proper routing. I’ve created an api module under modules/. Extended the CUrlManager class to use different parameters like the urlFormat to ‘path’ ‘enableCsrfValidation’=>false, etc.

Yet I can’t seem to get the following routes to work:

GET          api/survey/<id>
DELETE   api/survey/<id>
PUT          api/survey/<id>

I need these routes which are the same other than the HTTP method. It is not desirable to have a “catch all” method to conditionally route them within the controller. I’ve tried setting them like this with no success:

['<controller>/get.', 'pattern'=>'api/<controller:\w+>/<id:\w*>', 'verb'=>'GET'],
['<controller>/delete', 'pattern'=>'api/<controller:\w+>/<id:\w*>', 'verb'=>'DELETE'],
['<controller>/update', 'pattern'=>'api/<controller:\w+>/<id:\w*>', 'verb'=>'UPDATE'],

But it is not working, independently of the HTTP verb. The error suggests Yii believes the parameter to be the “action” sighs . If I call api/survey/1 this is what I get.

The system is unable to find the requested action “1”.

I have checked that Yii does not handle OPTIONS verb / http method, so what would be the proper way to handle CORS pre-flight? I sense I will have to hack the framework to have it working :\

Thank you in advance, I welcome any help.


Hi Rodrigo.

I built this for Yii2, if you like the concept I will try to find my Yii1 version.
You can use MySQL to add as many alias links as needed.

Sounds like a webhook scenario.
You would add an entry /survey and then capture the ID and do whatever is needed then.