Zend Framework 2 RESTful Controller Actions

After numerous attempts, I cannot get my rest functions to work in my test application.

I wondered if anyone has any experience with the RestfulController class in Zend FrameWork 2.0.0beta3.

I implemented methods from the abstract class RestfulController, let the getList () method echo "Foo", made a curl request to get some result, but all I get is a blank screen.

I know there are options for zend framework 1.x, but for my project I have to use 2.x.

If any of you would offer me any help that would be very grateful!

+4
source share
3 answers

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', ), ); /** * You may centralized this process through controller event callback handler */ $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'; } // continue for xml, amf etc. $response->headers()->addHeaderLine('Content-Type',$contentType); $adapter = new $adapter; $response->setContent($adapter->serialize($content)); return $response; } // other actions continue ... } } 

also do not forget to register your module in the application configuration

+5
source

I can’t say how you implemented it with current information, but Restful should work fine with ZF2. I worked in beta2.

  • Make sure that your controller extends the RestfulController and that your routes correctly configure the controller and identifier parameters, i.e. '/ [: Controller [/ [: identifier]]]'. Use "Zend \ Mvc \ Router \ Http \ Segment" as the type of route.
  • Using curl with the HTTP GET method, and id should not call the getList () method. If an identifier is specified, it will call get ($ id).
  • Instead of echoing, try returning the array.

You can also take a look at the ZF2 Restful Module Skeleton on GitHub for inspiration.

+3
source

Consider viewing these ZF2 modules:

In particular, the Module.php and config / module.config.php files can help.

+3
source

Source: https://habr.com/ru/post/1400213/


All Articles