What exactly does rendering / response strategy mean in Zend Framework 2 and how to use it?
Asked Answered
A

1

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?

Accelerant answered 25/6, 2013 at 7:56 Comment(3)
A little aside the scope of your question, but what you need has actually been converted into a ControllerPlugin which is called acceptableViewModelSelector(). This should cover what you need ;)Shemeka
Hey Sam! Thank you for the hint! Actually I've implemented it with the 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).Accelerant
not too familiar with it, sorry. Multiple accept headers isn't really my region :DShemeka
F
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.

Fleabitten answered 25/7, 2013 at 16:11 Comment(1)
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.Portia

© 2022 - 2024 — McMap. All rights reserved.