rest api with cakephp
TRANSCRIPT
REST API & CakePHPAnuchit Chalothorn
Agenda
● REST● Route● Resource Mapping
REST
a style of software architecture for distributed systems such as the WWW. The REST language uses nouns and verbs, and has an emphasis on readability. Unlike SOAP, REST does not require a message header to and from a service provider.
Concept
● the base URI for the web service, such as http://example.com/resources/
● the Internet media type of the data supported by the web service.
● the set of operations supported by the web service using HTTP methods (e.g., GET, PUT, POST, or DELETE).
● The API must be hypertext driven.
Example URI
● http://example.org/user/● http://example.org/user/anuchit● http://search.twitter.com/search.json?q=xxx
Example methods
Resource GET PUT POST DELETE
http://example.org/user list collection replace create delete
http://example.org/user/rose list data replace/ create ? / create delete
Simple Diagram
Requester Provider
GET /user/anuchit HTTP/1.1
200 with some data
No "official" standard
There is no "official" standard for RESTful web services, This is because REST is an architectural style, unlike SOAP, which is a protocol.
Shortcut - Web Services design
● Choose method old style, new style● Look around an eco-system● Who'll using your services● How to implementation● Design and document
REST & CakePHP
The fastest way to get up and running with REST is to add a few lines to your routes.php file The Router object features a method called mapResources(), that is used to set up a number of default routes for REST access to your controllers.
Route
If we wanted to allow REST access to a recipe database, we’d do something like this:
//In app/Config/routes.php...
Router::mapResources('recipes');Router::parseExtensions();
HTTP REQUEST Methods
HTTP Format URL.Format Controller action invoked
GET /recipes.format RecipesController::index()
POST /recipes.format RecipesController::add()
PUT /recipes/123.format RecipesController::edit(123)
DELETE /recipes/123.format RecipesController::delete(123)
POST /recipes/123.format RecipesController::edit(123)
REST & CakePHP
The fastest way to get up and running with REST is to add a few lines to your routes.php file The Router object features a method called mapResources(), that is used to set up a number of default routes for REST access to your controllers.
Controller (1)
A basic controller might look something like this
// Controller/RecipesController.phpclass RecipesController extends AppController {
public $components = array('RequestHandler');
public function index() { $recipes = $this->Recipe->find('all'); $this->set(array( 'recipes' => $recipes, '_serialize' => array('recipes') )); }
Controller (2)
public function view($id) { $recipe = $this->Recipe->findById($id); $this->set(array( 'recipe' => $recipe, '_serialize' => array('recipe') )); }
Controller (3)
public function edit($id) { $this->Recipe->id = $id; if ($this->Recipe->save($this->request->data)) { $message = 'Saved'; } else { $message = 'Error'; } $this->set(array( 'message' => $message, '_serialize' => array('message') )); }
Controller (4)
public function delete($id) { if ($this->Recipe->delete($id)) { $message = 'Deleted'; } else { $message = 'Error'; } $this->set(array( 'message' => $message, '_serialize' => array('message') )); }
View
Since we’ve added a call to Router::parseExtensions(), the CakePHP router is already primed to serve up different views based on different kinds of requests.
Modify View (1)
If we wanted to modify the data before it is converted into XML we should not define the _serialize view variable and instead use view files. We place the REST views for our RecipesController inside app/View/recipes/xml.
Modify View (2)
// app/View/Recipes/xml/index.ctp// Do some formatting and manipulation on// the $recipes array.$xml = Xml::fromArray(array('response' => $recipes));echo $xml->asXML();
Resource Mapping
If the default REST routes don’t work for your application, you can modify them using Router::resourceMap().
Router::resourceMap(array( array('action' => 'index', 'method' => 'GET', 'id' => false), array('action' => 'view', 'method' => 'GET', 'id' => true), array('action' => 'add', 'method' => 'POST', 'id' => false), array('action' => 'edit', 'method' => 'PUT', 'id' => true), array('action' => 'delete', 'method' => 'DELETE', 'id' => true), array('action' => 'update', 'method' => 'POST', 'id' => true)));
Thank You