What is RESTful development and how easy to develop a RESTful web application using cakephp framework?
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. So, let’s explore the question of what is RESTful development and how easy to develop a RESTful web application using cakephp framework?
What is RESTful Development?
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
In traditional MVC based framework a URL would specify a resource and its action. For example:
http://www.mydomain.com/articles/show/5
This URL will tell the framework to display the details of 5th number article by using show action/method of article controller. But in REST architecture URL don’t specify the action, URL only specify the resource and the action of resource is determined by HTTP method used with which the request is submitted. For example, in RESTful architecture the above URL would be:
http://www.mydomain.com/articles/5
This URL would be submitted using the HTTP GET method, in RESTful based architecture the framework will route it to show method of article resource(controller) by determining that request is submitted by using GET method.
The table below shows how various actions performed on a resource are mapped to URLs and HTTP methods.
| HTTP Method | URL | Action / Method |
| GET | http://www.mydomain.com/articles/5 | show |
| DELETE | http://www.mydomain.com/articles/5 | delete |
| PUT | http://www.mydomain.com/articles/5 | update |
| POST | http://www.mydomain.com/articles/ | create |
Note - The URL for performing show, delete, and update on a resource is same and are routed to the correct action of article controller based on the HTTP method that is used to submit them.
What are the advantages of RESTful development or RESTful architecture based development?
It is very hard to describe every benefit of a RESTful architecture based development but here I am going to describe few one
Well Defined architecture Design – RESTful architecture define a standard way of implementing controllers and access to models in a web application. you would apply this pattern throughout your web application’s architecture. Every controller would consist of the same standard set of methods show, delete, update, and create. The application framework will route to the correct method based on looking at both the URL and the HTTP method used for incoming requests. You have great consistency in your web applications. All of your controllers are implemented in the same style and contain the same set of methods.
REST is a web service Architecture – The REST architecture provides an excellent platform for providing API interface as web service, as in RESTful architecture every resource can be manipulated by using HTTP methods and a resource can have multiple representations. For example, considering the book example again, you could easily imagine a web service API that used URLs identical to that which a browser uses to get HTML content.
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:
http://www.mydomain.com/article/5
http://www.mydomain.com/article/5.xml
The first request would return an HTML representation of the article resource. The second request would return an XML representation of the article resource. This is another advantage of a RESTful architecture. The same controllers and actions can be used to deliver a variety of response (HTML, RSS, XML, etc). This makes implementing web services in a RESTful architecture extremely easy, and again maintains a consistent design style.
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 (in our article example it would be www.mydomain.com/articles/view/1).
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:
- 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.
- Add Router::parseExtensions(); line after Router::mapResources(‘article’);
- Include RequestHandler component in article controller [var $components = array('RequestHandler');]
- 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).
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. I am very thankful to cakephp community & development team who has developed and provided such a nice framework which makes our life quite easy for developing RESTful API or doing RESTful development (RESTFul api development).
Hope this posting helped you, if you have any suggestion, question feel free to contact me at rakesh@rightwaysolution.com







