Revision #16 was created by pligor on Apr 3, 2012, 11:15:28 AM.
bug fixes and minor changes necessary to work with Backbone (one the best javascript frameworks)
Tags
REST, API, Tutorial, Backbone
Content
[...]
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,
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>.",