RESTful Web Development with CakePHP

12
RESTful web development with CakePHP Presented by Andrew Weir

description

RESTful Web Development with CakePHP from CakePHP Toronto Meetup on November 23rd 2010. http://andrw.net/blog/cakephp-toronto-meetup

Transcript of RESTful Web Development with CakePHP

Page 1: RESTful Web Development with CakePHP

RESTful web development

with CakePHP

Presented by Andrew Weir

Page 2: RESTful Web Development with CakePHP

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

http://en.wikipedia.org/wiki/REST

Page 3: RESTful Web Development with CakePHP

Most companies online who offer APIs and webservices use REST.

Who’s using REST?

Click icon to add pictureClick icon to add picture

Page 4: RESTful Web Development with CakePHP

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.

Page 5: RESTful Web Development with CakePHP

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.

http://book.cakephp.org/view/1238/REST

Page 6: RESTful Web Development with CakePHP

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)

Page 7: RESTful Web Development with CakePHP

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]+")

);

Page 8: RESTful Web Development with CakePHP

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.

Page 9: RESTful Web Development with CakePHP

Setting up views for additional content typesCakePHP 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

Page 10: RESTful Web Development with CakePHP

Consuming RESTful APIs with CakePHP

CakePHP’s build in HttpSocket class works great for consuming RESTful APIs and it’s easy to use!

http://book.cakephp.org/view/1517/HttpSocket

http://api.cakephp.org/class/http-socket

Neil Crookes released an Oauth extension to Cake’s built in HttpSocket.

http://www.neilcrookes.com/2010/04/12/cakephp-oauth-extension-to-httpsocket/

Page 11: RESTful Web Development with CakePHP

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.

https://github.com/kvz/cakephp-rest-plugin

Page 12: RESTful Web Development with CakePHP

The End

Visit my blog http://andrw.net and follow me on twitter @andruu