RESTful web application development in cakephp

This article is also available in the following languages:
By rightwayindia
Nowadays RESTful architecture based API (web services) development is accepted across the Web as a simpler alternative to SOAP - and Web Services Description Language (WSDL)-based Web services. Key evidence of this shift is the adoption of REST by web 2.0 service providers, including Yahoo, Google, and Facebook, who have shifted their SOAP and WSDL-based web services to RESTFul API. RESTful development has become the standard way of creating a web application.
Representational State Transfer (REST) is a stateless client-server architecture in which the resources are identified by their URLs and are manipulated through their representations, this basically means that each unique URL is a representation of some object (resource). Within the REST architecture, requests from the Web service clients or browser use standard HTTP methods to manipulate the application's resources. As far as REST is concerned GET, POST, PUT, and DELETE HTML methods are used to manipulate resources. However, the HTTP protocol defines eight methods, GET, POST, PUT, DELETE, HEAD, TRACE, OPTIONS, and CONNECT

Multiple representations for a resource - When you request a page using a RESTful architecture, the page that is returned can be considered a representation of the resource that you are requesting. However, an HTML page is just one possible representation of any given resource. Other representations might include an XML document, a text document, or a block of JSON encoded JavaScript. Using the RESTful architecture, you would request a different representation of a resource using the same method, but by passing a different piece of metadata to the server indicating the representation that you would like to have returned. For example the following two requests would both be routed to the same controller and action method:

How easy to develop a RESTful architecture based web application using cakephp framework?
The power of CakePHP has a lot to do with conventions. The framework enforce certain conventions and standards that users must follow. You name your database tables, file names, etc; a particular way and boom, models, views and controllers are automatically created and ready for use. This is the beauty of the MVC structure. Your URLs also follow thing structure: www.mydomain.com/controller/action/params

According to the introduction of a controller given in cookbook of cakephp, A controller is used to manage the logic for a part of your application. Most commonly, controllers are used to manage the logic for a single model. For example, if you were building a site for an online bakery, you might have a RecipesController and a IngredientsController managing your recipes and their ingredients. Controllers can include any number of methods which are usually referred to as actions. An action is a single method of a controller. CakePHP’s inbuilt dispatcher calls actions when an incoming request matches a URL to a controller’s action

You can easily add REST functionality to your application with only a few changes, below are all changes which you need to make to get REST benefits:

1) Open app/config/routes.php file and add
Router::mapResources(‘article’); line at the end of the file (here article is a resource and a controller of the application), this line will map GET, POST, PUT, DELETE HTML methods with index, view(show), add(creation), edit(update), delete methods/actions of article resource (controller). This line has to be repeated for every resource. So this single line has done the half job for making an RESTful architecture based web application development. Now only thing which remains for complete RESTful development is multiple representations of a resource. This is also very smartly handled in cakephp framework and described below in 2rd, 3rd and 4th points.

2) Add Router::parseExtensions(); line after
Router::mapResources(‘article’);
3) Include RequestHandler component in article controller var $components = array('RequestHandler');
4) At last for every alternative type of representations of a resource you need add a directory under views/articles and views/layouts, the name of the directory would be extension such as txt, pdf, doc etc…(note – that in case of xml you don’t have to create directory under views/layouts as this is already provided by cakephp standard installation).

5) If you want to customize how the url maps to any action in your RESTful api you can add a line like this to your routes.php file

Router::connect(
    "/:controller/:id",
    array("action" => "edit", "[method]" => "PUT"),
    array("id" => "[0-9]+")
)
Where [method] can be one of the HTTP verbs already mentioned.

Once you follow above 4 points as described, you will have RESTful architecture ready for your application. So it is quite easy to develop a REST based web application using cakephp framework.

Comments

  • Posted 08/17/10 08:45:34 AM
    please give us tutorial how to implement this REST app in client, i'm little confuse about route configuration if i want to use action add, edit, delete, view, and index ... i've success to implement using cake normal route using cake http request with cake url ex : http://localhost/myclientuser/posts , but according to this blog article (http://www.blog.rightwaysolution.com/develop-a-restful-web-application-using-cakephp-framework/) that REST app should using customize url route

    Router::connect(
    "/:controller/:id",
    array("action" => "edit", "[method]" => "PUT"),
    array("id" => "[0-9]+")
    )

    i try using method put,delete,get using method based on (http://api.cakephp.org/class/http-socket) but doesn't seem to work,

    my route are like this (using uuid)

    Router::connect("/:controller/:id",array("action" => "edit", "[method]" => "PUT"),array("id" => "/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/"));
    Router::connect("/:controller/:id",array("action" => "view", "[method]" => "GET"),array("id" => "/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/"));

    please help .... thanks :)
  • Posted 01/13/10 05:54:48 AM
    Yesterday I did a presentation on the REST plugin I mentioned earlier:
    http://kevin.vanzonneveld.net/techblog/article/cakephp_rest_plugin_presentation/
  • Posted 01/11/10 01:36:07 AM
    TO know more about RESTful development and its benefits look at my details article posted on http://www.blog.rightwaysolution.com/develop-a-restful-web-application-using-cakephp-framework/
  • Posted 01/07/10 04:57:11 AM
    it is very helpful to create the application which provide the simple facility like web services Ex: product info,price info etc.
  • Posted 01/06/10 09:59:09 AM
    The issue I encountered building a REST-ish API in CakePHP is that I didn't want to force my client implementations to create Cake-centric POST data structures. As the data coming in was very simple, I instead implemented logic to generically modify POST to take a simple name=value POST and cake-ify it.
    • Posted 01/06/10 11:36:13 PM
      The issue I encountered building a REST-ish API in CakePHP is that I didn't want to force my client implementations to create Cake-centric POST data structures. As the data coming in was very simple, I instead implemented logic to generically modify POST to take a simple name=value POST and cake-ify it.
      yes in that situation we also can build few more function which just take XML data from client and take them into Array as per cakePHP structure. this way both side will be satisfied.
  • Posted 01/06/10 08:37:53 AM
    Not finished, but is aimed to be a drop in REST plugin to unlock all your existing controller actions in a RESTful way

    http://github.com/kvz/cakephp-rest-plugin
    Takes json, credentials, views (and passing variables to them) out of your hands.

Comments are closed for articles over a year old