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

Changes

Title unchanged

How-To: Create a REST API

Category unchanged

How-tos

Yii version unchanged

Tags unchanged

REST, API, Tutorial, Backbone

Content changed

This article will explain how to create a REST API with the Yii framework. ## Information about REST A good introduction about implementing REST service with PHP can be found on [http://www.gen-x-design.com](https://web.archive.org/web/20130910164802/http://www.gen-x-design.com/archives/create-a-rest-api-with-php/).

## Usefull Tools
[...]
// Try to save the model
if($model->save())
$this->
_sendResponse(200, CJSON::encode($model));
else
// prepare the error $msg
[...]
$num = $model->delete();
if($num>0)
$this->
_sendResponse(200, $num); //this is the only way to work with backbone
else
$this->_sendResponse(500,
[...]
How are the API responses actually sent? Right, we need to implement the _sendResponse method.

This code is borrowed from [http://www.gen-x-design.com/archives/create-a-rest-api-with-php](
https://web.archive.org/web/20130910164802/http://www.gen-x-design.com/archives/create-a-rest-api-with-php/).


```php
private function _sendResponse($status = 200, $body = '', $content_type = 'text/html')
{
[...]
98 2
123 followers
Viewed: 506 605 times
Version: 1.1
Category: How-tos
Written by: jwerner
Last updated by: Rohit Suthar
Created on: Apr 15, 2011
Last updated: 10 years ago
Update Article

Revisions

View all history