RESTful Web Development with CakePHP


Published on

RESTful Web Development with CakePHP from CakePHP Toronto Meetup on November 23rd 2010.

Published in: Technology
1 Comment
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

RESTful Web Development with CakePHP

  1. 1. What is REST? REST (Representational State Transfer) is a style of software architecture mainly used for web services to communicate between servers and clients using existing features of the HTTP protocol. The main HTTP verbs/methods used in REST are GET, POST, PUT and DELETE
  2. 2. Most companies online who offer APIs and webservices use REST. Who’s using REST?
  3. 3. Why use REST? You’ve already wrote the code! Use your existing code with little changes. Open your application to the public. Allow other developers to integrate your application into their projects or extend the functionality of your application. Create applications on multiple platforms. Using your REST API you can build mobile and desktop applications using your existing code base.
  4. 4. REST in CakePHP REST in CakePHP is simple. Add RESTful controllers to Router::mapResources() to set up default routes in /config/router.php. Add desired extensions to Router::parseExtension() eg. xml, json, yaml, etc. in /config/router.php. Add RequestHandler component to controllers with REST functionality.
  5. 5. CakePHP RESTful routing HTTP Method URL Controller Action GET /recipes.ext RecipesController::index() GET /recipes/123.ext RecipesController::view(123) POST /recipes.ext RecipesController::add() PUT /recipes/123.ext RecipesController::edit(123) DELETE /recipes/123.ext RecipesController::delete(123) POST /recipes/123.ext RecipesController::edit(123)
  6. 6. Custom RESTful routing If you have additional methods in your controller that don’t correspond to the default CRUD functionality you can add additional routes for those methods. Router::connect( "/:controller/:id", array("action" => ”custom_action", "[method]" => "PUT"), array("id" => "[0-9]+") );
  7. 7. Specifying HTTP methods CakePHP's Router class uses a number of different indicators to detect the HTTP method being used. Here they are in order of preference: The _method POST variable The X_HTTP_METHOD_OVERRIDE The REQUEST_METHOD header The _method POST variable is helpful in using a browser as a REST client (or anything else that can do POST easily). Just set the value of _method to the name of the HTTP request method you wish to emulate.
  8. 8. Setting up views for additional content types CakePHP knows when using Router::parseExtensions() to look for content types in a child directory named after the content type. /views/[object]/[content type]/[action].ctp If we want to create an index action for our recipe controller with a content type of json we would create the following file: /views/recipes/json/index.ctp Don’t forget to create a default layout for the content type as well. /views/layouts/[content type]/default.ctp
  9. 9. Consuming RESTful APIs with CakePHP CakePHP’s build in HttpSocket class works great for consuming RESTful APIs and it’s easy to use! Neil Crookes released an Oauth extension to Cake’s built in HttpSocket. extension-to-httpsocket/
  10. 10. CakePHP REST Plugin Kevin van Zonneveld (kvz) has released and maintains a REST plugin that features authentication, rate-limiting per IP, automatic generation of xml and json views and support for callback methods.
  11. 11. The End Visit my blog and follow me on twitter @andruu