Posts

Showing posts from May, 2012

How to create Restful API in Zend Framework 2

Your ZF module just needs to provide Controller that speaks REST (for example RestfulController) and make sure that generated response will be formatted in JSON, in other words configure JSON rendering strategy. Below you can see an example controller. Note that it has convenient methods for to get all elements, single element, create/update and delete an element: use Zend\Mvc\Controller\RestfulController; class UserController extends RestfulController { public function getList() { return array("users" => array()); } public function get($id) { return array("id" => $id); } public function create($data) { return array("created" => "yes"); } public function update($id, $data) { return array("updated" => "yes"); } public function delete($id) { return array("deleted" => $i); } } When associating this controller to route, make sure to NOT use 'action' parameter. Otherwis