REST API with CakePHP

18,306 views

Published on

Published in: Technology, Self Improvement
2 Comments
20 Likes
Statistics
Notes
No Downloads
Views
Total views
18,306
On SlideShare
0
From Embeds
0
Number of Embeds
1,421
Actions
Shares
0
Downloads
213
Comments
2
Likes
20
Embeds 0
No embeds

No notes for slide

REST API with CakePHP

  1. 1. REST API & CakePHP Anuchit Chalothorn anuchit@redlinesoft.net
  2. 2. Agenda ● REST ● Route ● Resource Mapping
  3. 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. 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. 5. Example URI ● http://example.org/user/ ● http://example.org/user/anuchit ● http://search.twitter.com/search.json?q=xxx
  6. 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. 7. Simple Diagram Requester Provider GET /user/anuchit HTTP/1.1 200 with some data
  8. 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. 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. 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. 11. 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();
  12. 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. 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. 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. 15. Controller (2) public function view($id) { $recipe = $this->Recipe->findById($id); $this->set(array( 'recipe' => $recipe, '_serialize' => array('recipe') )); }
  16. 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. 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. 18. 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.
  19. 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. 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. 21. 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) ));
  22. 22. Thank You

×