在 Zend 中将数据发送到 MVC 视图的正确方法
在我们的设置中,我们为所有数据库对象提供模型+映射器。然后是控制器操作,它们根据业务逻辑为各个操作准备模型对象。
我们已将整个模型对象发送到视图,如果视图(html)想要显示名字,它可以调用 $obj->getFirstName() 或者如果其他视图(pdf)甚至可以调用$obj->getFullName()
。这是应该做的吗?
如果国家留空并且视图无知地调用 $obj->getCountry()->getISO3Code()
将是致命的,因为 getCountry()
返回 false 而不是预期的国家对象。
一种选择是用 IF.. 等干扰视图,这样就安全了。但这是否违背了视图应该在没有逻辑的情况下转储的目的?或者也许我压力太大了。
我们应该将整个模型对象发送到视图(就像现在一样)还是安全地准备并发送一组可查看字段?它有点让动作了解 PDF 视图的外观和 html 视图的外观,这可能再次违背了控制器的目的。
In our setup, we have models+mappers for all db objects. Then there are controller actions which prepare model objects for respective actions based on business logic.
We have send entire model object to the view and if view (html) wants to show first name, it can call $obj->getFirstName()
or if some other view (pdf) can even call $obj->getFullName()
. Is this how it is supposed to be done?
What if country was left empty and the view ignorantly calls $obj->getCountry()->getISO3Code()
will be fatal since getCountry()
returned false instead of a expected country object.
One option is to bother the view with IF.. etc so it is made safe. but does it not defeat the purpose that views should be dump without logic? or maybe I over stressed it.
should we send the entire model object to the view (as now) or safely prepare and send a array of viewable fields? It kinda it makes the action to be aware how PDF view looks like and html view looks like, again maybe defeating controllers purpose.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我承认我也在为同样的问题而苦苦挣扎。当控制器/操作在视图中设置值时 -
$this->view->someKey = 'someValue'
- 那么有一个隐含的期望,即控制器知道什么 视图需要。我想一般的想法是这样就可以了;视图负责如何呈现它传递的数据。在视图脚本中使用 if 语句没有任何问题。 看到类似以下内容是很常见的:
在视图脚本中 例如,看一下与分页控件关联的部分。
我创建了视图模型对象 - 一种旨在在视图中使用的模型的只读版本 - 这允许我在视图脚本中做一些更干净的事情。例如,您可以拥有一个具有诸如
hasCountry()
之类的方法的视图模型对象,以便您的视图脚本可以执行以下操作:这是一个简单的示例,但对于有关更复杂的逻辑当我尝试渲染实体时,我发现像这样的视图模型为一些特定于渲染的逻辑提供了一个家,这些逻辑在控制器中感觉不正确,并且对于视图脚本来说似乎有点复杂。
I confess that I struggle with the same question. When the controller/action sets values in the view -
$this->view->someKey = 'someValue'
- then there is an implicit expectation that that the controller is aware of what the view requires. I guess the general idea is that this is ok; the view is responsible for how to render the data it is passed.There is nothing wrong with using
if
statements inside your view-scripts. It is pretty common to see something like:in a view-script. For example, take a look at the partials associated to a pagination control.
I have created view-model objects - kind of a read-only version of my model intended for use in a view - that permits me to do things in a view-script that are a bit cleaner. For example, you could have a view-model object with a method like
hasCountry()
, so that your view-script could do something like:Kind of a trivial example, but for more complex logic about the entity I am trying to render, I find that a view-model like this provides a home for some of that rendering-specific logic that doesn't feel right in the controller and seems a bit complex for a view-script.