Tuesday, November 30, 2010

Create RESTful Applications Using The Zend Framework

The Zend Framework 1.9 release added a new feature - Zend_Rest_Controller. Zend_Rest_Controller and Zend_Rest_Route classes go hand in hand. In the previous versions of the Zend Framework, we have had the Zend_Rest_Server component. We still have. Since Zend_Rest_Server provides an RPC like component violating the REST architectural constraint, it is likely to be deprecated in the future versions of the Zend Framework.
In this article let us explore how to make use of Zend_Rest_Route and Zend_Rest_Controller to build a RESTful server application. Zend_Rest_Route routes the request to the appropriate module, controller and action depending on the HTTP request method and URI.
Let's start coding. We build the RESTful application based on the QuickStart project.

Adding the Zend_Rest_Route in the bootstrap

You can choose to enable Zend_Rest_Route for the entire application or for specific set of modules. In this example we enable the rest route for the entire application.
Bootstrap the front controller resource and add the rest route.

<?phpprotected function _initRestRoute()
{
        
$this->bootstrap('frontController');
        
$frontController Zend_Controller_Front::getInstance();
        
$restRoute = new Zend_Rest_Route($frontController);
        
$frontController->getRouter()->addRoute('default'$restRoute);

}
?>

Creating the Zend_Rest_Controller

Create the file application/controllers/ArticleController.php. We extend Zend_Rest_Controller instead of Zend_Controller_Action. In our controller we are going to have five actions.
  1. indexAction - return all articles
  2. getAction - return a particular article
  3. postAction - create a new article
  4. putAction - update a particular article
  5. deleteAction - delete a particular article
These actions are defined as abstract methods in the Zend_Rest_Controller class.
The skeletal controller looks like:

<?phpclass ArticleController extends Zend_Rest_Controller{

    public function 
init()
    {
        
$this->_helper->viewRenderer->setNoRender(true);
    }

    public function 
indexAction()
    {
    }

    public function 
getAction()
    {
    }
   
    public function 
postAction()
    {
    }
   
    public function 
putAction()
    {
    }
   
    public function 
deleteAction()
    {
    }

}
?>

For the purpose of brevity, I have disabled the view for this controller in the init() hook. To test the routing of the requests, let's append sample messages to the response object in each action.

<?php
 
public function indexAction()
    {
         
$this->getResponse()
            ->
appendBody("From indexAction() returning all articles");
    }

    public function 
getAction()
    {
        
$this->getResponse()
            ->
appendBody("From getAction() returning the requested article");

    }
   
    public function 
postAction()
    {
        
$this->getResponse()
            ->
appendBody("From postAction() creating the requested article");

    }
   
    public function 
putAction()
    {
        
$this->getResponse()
            ->
appendBody("From putAction() updating the requested article");

    }
   
    public function 
deleteAction()
    {
        
$this->getResponse()
            ->
appendBody("From deleteAction() deleting the requested article");

    }
?>

Testing the RESTful server using curl

On my computer I have installed this application for the domain zfrest.example.com. We use the curl command to test our RESTful server.
Testing the indexAction() : The URI http://zfrest.example.com/article represents the article resource. All articles are returned for this request.
$ curl http://zfrest.example.com/article
I get the following output:

From indexAction() returning all articles
Testing the getAction() : The URI http://zfrest.example.com/article/1 represents the resource - article 1.

$ curl http://zfrest.example.com/article/1
I get the following output:

From getAction() returning the requested article
Testing the postAction() : we make an HTTP POST request to http://zfrest.example.com.
$ curl -d "article=myarticle" http://zfrest.example.com/article/
I get the following output:

From postAction() creating the requested article
Testing the putAction() : we request the article 1 to be updated by making HTTP PUT request to http://zfrest.example.com/article/1

$ curl -d "article=updatedarticle" -X PUT http://zfrest.example.com/article/1
I get the following output:

From putAction() updating the requested article
Testing the deleteAction() : we send an HTTP DELETE request to http://zfrest.example.com/article/1
curl -X DELETE http://zfrest.example.com/article/1
I get the following output:

From deleteAction() deleting the requested article

Summarizing the exercise

Zend Framework allows you to build RESTful server applications using the Zend_Rest_Controller component. The curl command is a very useful tool to test RESTful servers. If you don't have the curl command on your computer, you can write a PHP script and make use of the curl extension provided by PHP. In the upcoming posts of this series, I will discuss managing API keys from your RESTful application, returning appropriate HTTP response codes, reading the body from PUT and DELETE requests and more.
Are you going to a build RESTful server using the Zend_Rest_Controller component? Tell me about your experiences.
Create RESTful Applications Using The Zend Framework - Part II : Using HTTP Response Code

No comments:

Post a Comment