REST API with CakePHP

download REST API with CakePHP

of 22

  • date post

    17-May-2015
  • Category

    Technology

  • view

    12.186
  • download

    6

Embed Size (px)

Transcript of REST API with CakePHP

  • 1. REST API & CakePHP Anuchit Chalothorn anuchit@redlinesoft.net

2. Agenda REST Route Resource Mapping 3. 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. 4. 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. 5. Example URI http://example.org/user/ http://example.org/user/anuchit http://search.twitter.com/search.json?q=xxx 6. 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 7. Simple Diagram Requester Provider GET /user/anuchit HTTP/1.1 200 with some data 8. 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. 9. 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 10. 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. 11. Route If we wanted to allow REST access to a recipe database, wed do something like this: //In app/Config/routes.php... Router::mapResources('recipes'); Router::parseExtensions(); 12. 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) 13. 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. 14. Controller (1) A basic controller might look something like this // Controller/RecipesController.php class RecipesController extends AppController { public $components = array('RequestHandler'); public function index() { $recipes = $this->Recipe->find('all'); $this->set(array( 'recipes' => $recipes, '_serialize' => array('recipes') )); } 15. Controller (2) public function view($id) { $recipe = $this->Recipe->findById($id); $this->set(array( 'recipe' => $recipe, '_serialize' => array('recipe') )); } 16. 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') )); } 17. Controller (4) public function delete($id) { if ($this->Recipe->delete($id)) { $message = 'Deleted'; } else { $message = 'Error'; } $this->set(array( 'message' => $message, '_serialize' => array('message') )); } 18. View Since weve added a call to Router:: parseExtensions(), the CakePHP router is already primed to serve up different views based on different kinds of requests. 19. 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. 20. 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(); 21. Resource Mapping If the default REST routes dont 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) )); 22. Thank You