何时应在 Zend Framework 2 中使用 AcceptableViewModelSelector 和渲染/响应策略? [英] When should AcceptableViewModelSelector and Rendering / Response Strategies be used in Zend Framework 2?

查看:54
本文介绍了何时应在 Zend Framework 2 中使用 AcceptableViewModelSelector 和渲染/响应策略?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果需要JSON、XML等输出格式,可以使用AcceptableViewModelSelector 控制器插件 或/和 (?) 替代渲染/响应策略.

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?

推荐答案

嗯,策略基本上是听众.根据控制器操作返回的模型类型提供操作的侦听器.如果你返回一个 ViewModelPhpRendererStrategy 将会发生.如果你返回一个 JsonModelJsonStrategy 会做它的事情,如果你返回一个 FeedModel 它是 FeedStrategy 做的事情.

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.

但是,这些策略仅在注册后才适用.因此,如果您在没有注册 JsonStrategy 的情况下返回 JsonModel,那么什么都不会发生.

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

AcceptableViewModelSelector 的存在是为了根据接受标头提供对不同 ModelFormat 的更轻松访问和处理.它有助于让事情变得更简单,不多也不少.

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.

策略,你也可以自己注册一个.有一些模块有一个 PdfStrategy,如果你告诉它,它会渲染出一个 PDF 文档.

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:

  • 从 ZF 2.0 开始就有策略
  • 控制器插件 AVMS 只是在 ZF 2.1 中的某个时候才出现,只是为了让事情变得更简单 - 这就是为什么它只不过是一个插件";)

如果这并不能真正涵盖您的问题,我对如何回答您感到很不安全^^

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

这篇关于何时应在 Zend Framework 2 中使用 AcceptableViewModelSelector 和渲染/响应策略?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆