Template for REST API controller in Yii 1
- Add the ApiController.php file to your controllers folder
- Change the
APPLICATION_ID
, and add your models in each action in the switch-case statement (remove the MyModel example before that) - Open the protected/config/main.php file and add this to your configuration:
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'caseSensitive' => true,
'rules' => array(
//API rules
array('api/list', 'pattern'=>'api/<model>', 'verb'=>'GET'),
array('api/view', 'pattern'=>'api/<model>/<id:\d+>', 'verb'=>'GET'),
array('api/update', 'pattern'=>'api/<model>/<id:\d+>', 'verb'=>'PUT'),
array('api/delete', 'pattern'=>'api/<model>/<id:\d+>', 'verb'=>'DELETE'),
array('api/create', 'pattern'=>'api/<model>', 'verb'=>'POST'),
),
),