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.
ControllerPlugin
which is calledacceptableViewModelSelector()
. This should cover what you need ;) – ShemekaAcceptableViewModelSelector
. But now I have two troubles with it: 1. If theAccept
contains several types, the request is automatically forwarded to thePhpRenderer
. 2. I cannot setscript_paths
for JSON / XML (in order to prepair the data for the output, s. here). – Accelerant