Book Image

Yii Project Blueprints

By : Charles R. Portwood ll
Book Image

Yii Project Blueprints

By: Charles R. Portwood ll

Overview of this book

Table of Contents (15 chapters)
Yii Project Blueprints
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Implementing CRUD actions


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;
}

Deleting users

The first method that we'll implement is our DELETE method. Remember that, for each method, we'll be hitting a single endpoint, /api/user/index , with different HTTP request types:

  1. The first change that we need to make is to our accessRules. We want only administrators to have...