I am working on applications of the same type, and so far it is working fine.
Routing:
'type' => 'Zend\Mvc\Router\Http\Segment', 'options' => array( 'route' => '/[:controller[.:format][/:id]]', 'constraints' => array( 'controller' => '[a-zA-Z][a-zA-Z0-9_-]*', 'format' => '(xml|json|sphp|amf)', 'id' => '[1-9][0-9]*', ), 'defaults' => array( 'controller' => 'Rest\Controller\IndexController', 'format' => 'json', ),
DI Attribute:
'alias' => array( 'index' => 'Rest\Controller\IndexController', ... )
in the controller, the type of content that you return for rendering depends on your own strategy, it can be achieved in different ways.
In my case, it should be able to respond in various formats, such as: php serialize , json , amf and xml , I use Zend\Serializer\Adapter to serialize my content and directly return an instance of the response, or you return it directly to the controller action, or centralized it by catching the RestfulController dispatch event and returning it through the callback handler.
Short review:
namespace Rest\Controller { use Zend\Mvc\Controller\RestfulController; class IndexController extends RestfulController { public function getList() { $content = array( 1 => array( 'id' => 1, 'title' => 'Title #1', ), 2 => array( 'id' => 2, 'title' => 'Title #2', ), ); $format = $this->getEvent()->getRouteMatch()->getParam('format'); $response = $this->getResponse(); if($format=='json'){ $contentType = 'application/json'; $adapter = '\Zend\Serializer\Adapter\Json'; } elseif($format=='sphp'){ $contentType = 'text/plain'; $adapter = '\Zend\Serializer\Adapter\PhpSerialize'; }
also do not forget to register your module in the application configuration