Now that we can authenticate and work with our API, we can work on implementing the four basic CRUD actions in a RESTful manner. The RESTful actions boil down to three main HTTP request types—GET
, POST
, and DELETE
. We'll implement each one for our users.
The first method we need to implement is our loadModel()
method. This method will be loaded in our User model and will throw the appropriate errors if something goes wrong:
private function loadModel($id=NULL) { if ($id == NULL) throw new CHttpException(400, 'Missing ID'); $model = User::model()->findByPk($id); if ($model == NULL) throw new CHttpException(400, 'User not found'); return $model; }