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.- indexAction - return all articles
- getAction - return a particular article
- postAction - create a new article
- putAction - update a particular article
- deleteAction - delete a particular article
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
From indexAction() returning all articles
$ curl http://zfrest.example.com/article/1
From getAction() returning the requested article
$ curl -d "article=myarticle" http://zfrest.example.com/article/
From postAction() creating the requested article
$ curl -d "article=updatedarticle" -X PUT http://zfrest.example.com/article/1
From putAction() updating the requested article
curl -X DELETE http://zfrest.example.com/article/1
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