Difference between #15 and #16 of
How-To: Create a REST API

Revision #16 has been created by pligor on Apr 3, 2012, 11:15:28 AM with the memo:

bug fixes and minor changes necessary to work with Backbone (one the best javascript frameworks)
« previous (#15) next (#17) »

Changes

Title unchanged

How-To: Create a REST API

Category unchanged

How-tos

Yii version unchanged

Tags changed

REST, API, Tutorial, Backbone

Content changed

[...]
default:
// Model not implemented error
$this->_sendResponse(501, sprintf(
'Error: Mode <b>list</b> is not implemented for model <b>%s</b>',
$_GET['model']) );
exitYii::app()->end(); } // Did we get some results? if(is_nullempty($models)) {
// No
$this->_sendResponse(200,
[...]
'Mode <b>view</b> is not implemented for model <b>%s</b>',
$_GET['model']) );
exitYii::app()->end();
}
// Did we find the requested model? If not, raise an error
[...]
$this->_sendResponse(404, 'No Item found with id '.$_GET['id']);
else
$this->_sendResponse(200, CJSON::encode($
_GET['model']));
}
```
[...]
sprintf('Mode <b>create</b> is not implemented for model <b>%s</b>',
$_GET['model']) );
exitYii::app()->end();
}
// Try to assign POST values to attributes
[...]
// Try to save the model
if($model->save())
$this->_sendResponse(200,

 
                $this->_getObjectE
CJSON::encoded($_GET['model'], $model->attributes) model));
else {
// Errors occurred
[...]
public function actionUpdate()
{
// Parse the PUT parameters

 
   
. This didn't work: parse_str(file_get_contents('php://input'), $put_vars);  
    $json = file_get_contents('php://input'); //$GLOBALS['HTTP_RAW_POST_DATA'] is not preferred: http://www.php.net/manual/en/ini.core.php#ini.always-populate-raw-post-data
 
    $put_vars = CJSON::decode($json,true); //true means use associative array


switch($_GET['model'])
[...]
sprintf( 'Error: Mode <b>update</b> is not implemented for model <b>%s</b>',
$_GET['model']) );
exitYii::app()->end(); } // Did we find the requested model? If not, raise an error if(is_null($model)$model === null)
$this->_sendResponse(400,
sprintf("Error: Didn't find any model <b>%s</b> with ID <b>%s</b>.",
[...]
// Try to save the model
if($model->save())
$this->
_sendResponse(200,
 
                sprintf('The model <b>%s</b> with id <b>%s</b> has been updated.',
 
                $_GET['model'], $_GET['id']) 
CJSON::encode($model));
else
// prepare the error $msg
[...]
sprintf('Error: Mode <b>delete</b> is not implemented for model <b>%s</b>',
$_GET['model']) );
exitYii::app()->end(); } // Was a model found? If not, raise an error if(is_null($model)$model === null)
$this->_sendResponse(400,
sprintf("Error: Didn't find any model <b>%s</b> with ID <b>%s</b>.",
[...]
$num = $model->delete();
if($num>0)
$this->
_sendResponse(200,
 
                sprintf("Model <b>%s</b> with ID <b>%s</b> has been deleted.",
 
                $_GET['model'], $_GET['id']) );
$num); //this is the only way to work with backbone
else
$this->_sendResponse(500,
[...]
// send the body
echo $body;
        exit;
 
}
// we need to create the body if none is passed
else
[...]
echo $body;
}
 
exit;
 
    }
Yii::app()->end();
}
```


### Getting the Status Codes
[...]
98 2
123 followers
Viewed: 498 519 times
Version: 1.1
Category: How-tos
Written by: jwerner
Last updated by: Rohit Suthar
Created on: Apr 15, 2011
Last updated: 9 years ago
Update Article

Revisions

View all history