Reply to comment

Zend Framework And RESTful Web Service

REST web api is not a new toy today. One of public service that user RESTful Architecture is twitter. On Zend Framework, we can implement RESTful easily. Just make action that related with HTTP method used.

getAction()  => handle request with HTTP GET
postAction() => handle request with HTTP POST
putAction()  => handle request with HTTP PUT
deleteAction()  => handle request with HTTP DELETE

For more details, please see these steps

Add Zend_Rest_Route Router on Front Controller
To add Zend_Rest_Route, you can add these code on bootstrap (application/Bootstrap.php)

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

Those code above will make the controller on application/ can be used for RESTful. To make RESTful on specific modules, just check Zend_Controller_Router documentation

Extends Controller From Zend_Rest_Controller
Make controller application/controllers/SampleController.php with extends from Zend_Rest_Controller. This controller will have 5 controller (indexAction(), getA ction(), postAction(), putAction(), deleteAction()). Those all are inheritance from Zend_Rest_Controller.

<?php
class SampleController extends Zend_Rest_Controller
{
    public function 
init()
    {
        
/* Initialize action controller here */
    
}

    public function 
indexAction()
    {
        echo 
"Get all data"PHP_EOL;
        
$this->_helper->viewRenderer->setNoRender(true);
    }

    public function 
getAction()
    {
        echo 
"Get data with ID " $this->_request->get('id'), PHP_EOL;
        
$this->_helper->viewRenderer->setNoRender(true);
    }

    public function 
postAction()
    {
        echo 
"Add new data "PHP_EOL;
        
print_r($_POST);
        
$this->_helper->viewRenderer->setNoRender(true);
    }

    public function 
putAction()
    {
        echo 
"Update data with ID " $this->_request->get('id'), PHP_EOL;
        
$this->_helper->viewRenderer->setNoRender(true);
    }

    public function 
deleteAction()
    {
        echo 
"Delete data with ID " $this->_request->get('id'), PHP_EOL;
        
$this->_helper->viewRenderer->setNoRender(true);
    }
}
?>

Let us test our code with curl or if you want to use GUI application can use rest-client

$ curl -X GET http://localhost/rest/sample/
Get all data
 
$ curl -X GET http://localhost/rest/sample/id/1
Get data with ID 1
 
$ curl -X DELETE http://localhost/rest/sample/id/1
Delete data with ID 1
 
$ curl -X POST -d "var=value" http://localhost/rest/sample
Add new data
Array
(
     [var] => value
)
 
$ curl -X PUT -d "var=value"  http://localhost/rest/sample/id/1
Update data with ID 1

We have see RESTful usage on Zend Framework. Basically, to receive data that sent use HTTP PUT is a litle difficult. To receive them easily, just add this code in _initRestRoute() method on application/Bootsrap.php

<?php
  $frontController
->registerPlugin(new Zend_Controller_Plugin_PutHandler());
?>

With add code above, data that be sent with HTTP PUT can be acessed as parameter on controller with use getParam() method. Add this code on putAction() in application/controllers/SampleController.php

<?php
  
echo $this->_request->getParam('var'), PHP_EOL;
?>

Let us test again with use HTTP PUT

$ curl -X PUT -d "var=value" UPDATE http://localhost/rest/sample/id/1
Update data with ID 1
value

From test above, we can see that putAction() display value from var variable that we sent.

Reply

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options