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 1We 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.
Comments
Thank you for the tutorial.
Thank you for the tutorial. It is very helpful to understand RESTfull Service.
Could you explain why I got the following curl: (6) message?
>curl -X POST -d "var=value" POST http://rest.local/sample
curl: (6) Could not resolve hot: POST; Host not found
Add new data
Array
(
[var] => value
)
Sorry, that is typo. The
Sorry, that is typo.
The second POST shouldn't exist.
And here the correct command
curl -X POST -d "var=value" http://rest.local/sample
Thanks
This is exactly what I was
This is exactly what I was thinking and i'm looking for more at google
HelloGuys! Nice to be here.
HelloGuys!
Nice to be here. Check this out for PHP Developers.
Providing some special features in php web development.
Hi guys I try to made a REST
Hi guys
I try to made a REST client/server application and I have some problems when I try to set up PUT method
here is the server side code
public function putAction()
{
$server = new Zend_Rest_Server();
$server->setClass('Application_Model_WsServer');
$server->handle();
}
and the client
$client = new Zend_Rest_Client('http://ws.example.com/article/id/2');
$result = $client->put();
print_r($result);
I get the following error message
"No Method Specified"
So I put the method name and param in the url as GET param like 'http://ws.example.com/article/id/2?method=method¶m=param1'
and it works
My questions are: is it correct to do that ? if not, what is the other way ?
thx
Post new comment