6

The ZF2 docu describes Creating and Registering Alternate Rendering and Response Strategies. I've read that, but I don't really understand, how to use a strategy.

I have an application, that should ship three types of output (JSON, XML, and HTML), depending on the HTTP header accept. How can I use the strategies for this case?

automatix
  • 14,018
  • 26
  • 105
  • 230
  • 1
    A little aside the scope of your question, but what you need has actually been converted into a `ControllerPlugin` which is called [`acceptableViewModelSelector()`](http://zf2.readthedocs.org/en/latest/modules/zend.mvc.plugins.html#zend-mvc-controller-plugins-acceptableviewmodelselector). This should cover what you need ;) – Sam Jun 25 '13 at 08:06
  • Hey Sam! Thank you for the hint! Actually I've implemented it with the [`AcceptableViewModelSelector`](http://framework.zend.com/manual/2.1/en/modules/zend.mvc.plugins.html#zend-mvc-controller-plugins-acceptableviewmodelselector). But now I have two troubles with it: 1. If the `Accept` contains several types, the request is automatically forwarded to the `PhpRenderer`. 2. I cannot set `script_paths` for JSON / XML (in order to prepair the data for the output, s. [here](http://stackoverflow.com/questions/17275230/how-to-handle-multidimensional-output-with-nested-lists-using-the-table-gatewa)). – automatix Jun 25 '13 at 08:38
  • not too familiar with it, sorry. Multiple accept headers isn't really my region :D – Sam Jun 25 '13 at 10:09

1 Answers1

5

The loose concept of a strategy in Zend Framework 2 is the behavior that the application should follow when events are triggered during the MVC application cycle, that being in fact event driven.

In more practical terms, a strategy is basically an event listener, usually a concrete instance of \Zend\EventManager\AbstractListenerAggregate, and usually listens to \Zend\Mvc\MvcEvent various events like EVENT_RENDER and EVENT_RENDER_ERROR.

The listener is attached to the \Zend\EventManager\EventManager and then, using the aformentioned \Zend\Mvc\MvcEvent to access all the fundamental resources of the MVC cycle (router, request, response, the application itself, etc), the listener can inspect the status of the application and alter its flow.

In the example provided by ZF2 official docs, the listener inspects the request's accept headers, selects a renderer and alters the response accordingly.

It is a bit old though, so I'd suggest to look at some better examples reading the code of the staple strategies provided by the framework, i.e. \Zend\Mvc\View\Http\RouteNotFoundStrategy which listens to EVENT_DISPATCH and EVENT_DISPATCH_ERROR to handle the rendering of 404 error pages.

Also it is vitally important that you understand how the whole EventManager works. Official docs for that are quite good, plus there is a lot of stuff about it if you google around.

Stefano Torresi
  • 686
  • 4
  • 10
  • 5
    A couple notes: a "strategy" usually addresses two events. The first is "render", the second "response". In the "render" event, the strategy will typically introspect the view model to determine if it is capable of rendering it. If so, it returns a renderer. The first strategy to return a renderer wins. The "response" event is used to take the results of rendering and push them into the response object. A common use case is to also inject specific headers, such as a Content-Type. – weierophinney Jul 25 '13 at 16:27