Frage

If one needs JSON, XML, etc. als output format, one can use the AcceptableViewModelSelector Controller Plugin or / and (?) alternate rendering / response strategies.

When should the one and when the other (and when a combination) be applied?

Can/should they be combined?

War es hilfreich?

Lösung

Well, the Strategies are basically listeners. Listeners that give action depending on what kind of Model will be returned from your Controllers actions. If you return a ViewModel the PhpRendererStrategy will take place. If you return a JsonModel, the JsonStrategy will do it's stuff and if you return a FeedModel it's the FeedStrategy doing it's thing.

However the Strategies only apply if they are registered. So if you return a JsonModel without having the JsonStrategy registered, then nothing will happen.

The AcceptableViewModelSelector exists to provide an easier access and handling of different ModelFormats depending on the accept header. It helps to make things easier, no more, no less.

Strategies, you can also register your own one. There's modules out there that have a PdfStrategy which will render out a PDF-Document if you tell it to.

To tune this down a little to your question:

  • Strategies have been there from the start of ZF 2.0
  • The controller plugin AVMS only got in sometime within ZF 2.1 to just make things easier - that's why it's nothing more than a "plugin" ;)

If that doesn't really cover your question, i'm quite insecure on how to answer you ^^

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top