Download - REST API with CakePHP

Transcript
Page 1: REST API with CakePHP

REST API & CakePHPAnuchit Chalothorn

[email protected]

Page 2: REST API with CakePHP

Agenda

● REST● Route● Resource Mapping

Page 3: REST API with CakePHP

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.

Page 4: REST API with CakePHP

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.

Page 5: REST API with CakePHP

Example URI

● http://example.org/user/● http://example.org/user/anuchit● http://search.twitter.com/search.json?q=xxx

Page 6: REST API with CakePHP

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

Page 7: REST API with CakePHP

Simple Diagram

Requester Provider

GET /user/anuchit HTTP/1.1

200 with some data

Page 8: REST API with CakePHP

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.

Page 9: REST API with CakePHP

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

Page 10: REST API with CakePHP

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.

Page 11: REST API with CakePHP

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();

Page 12: REST API with CakePHP

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)

Page 13: REST API with CakePHP

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.

Page 14: REST API with CakePHP

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

Page 15: REST API with CakePHP

Controller (2)

public function view($id) { $recipe = $this->Recipe->findById($id); $this->set(array( 'recipe' => $recipe, '_serialize' => array('recipe') )); }

Page 16: REST API with CakePHP

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

Page 17: REST API with CakePHP

Controller (4)

public function delete($id) { if ($this->Recipe->delete($id)) { $message = 'Deleted'; } else { $message = 'Error'; } $this->set(array( 'message' => $message, '_serialize' => array('message') )); }

Page 18: REST API with CakePHP

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.

Page 19: REST API with CakePHP

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.

Page 20: REST API with CakePHP

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();

Page 21: REST API with CakePHP

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

Page 22: REST API with CakePHP

Thank You