RESTful Web Development with CakePHP
-
Upload
andru-weir -
Category
Technology
-
view
19.701 -
download
0
description
Transcript of RESTful Web Development with CakePHP
RESTful web development
with CakePHP
Presented by Andrew Weir
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
Most companies online who offer APIs and webservices use REST.
Who’s using REST?
Click icon to add pictureClick icon to add picture
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.
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
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)
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]+")
);
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.
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
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/
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