php – Zend View Helper中的数据库连接

在Zend View Helper中使用数据库映射器是一种好习惯吗?因为在我的情况下,这个帮手正在给我一个< div>的盒子.不断变化,并在我的应用程序中的所有视图中显示.我无法将该对象从控制器中的数据库加载,并将其分配给每次查看.

如果有人在使用zend视图帮助程序时可以告诉良好的编程习惯,那将非常有用:

>如果可以通过$this-> view->变量= …来为View Helpers中的视图指定内容.
>如果可以在View Helpers中创建和使用模型.
>如果可以在View Helper中使用Zend_View可用的方法,例如通过执行$this-> view-> baseUrl(‘…’);

最佳答案 你的第二和第三个要点对我来说似乎是正确的,只要你不从你的观点中对你的模型做任何逻辑性的东西.模型和视图之间的链接必须是只读的.

关于你的第一点,你不需要为视图分配任何东西,你查看助手应该直接将HTML输出返回到视图.

关于第一个问题,您可以创建专门用于此任务的View Helper,这样您就可以将它用作视图助手和映射器之间的简单代理.一个视图助手将允许您访问任何映射器,而其他视图助手可以使用此视图助手来获取映射器.

让我们看看Trygve Reenskaug对MVC的看法:

Models

Models represent knowledge. A model could be a single object
(rather uninteresting), or it could be some structure of objects.

There should be a one-to-one correspondence between the model and its
parts on the one hand, and the represented world as perceived by the
owner of the model on the other hand.

Views

A view is a (visual) representation of its model. It would
ordinarily highlight certain attributes of the model and suppress
others. It is thus acting as a presentation filter.

A view is attached to its model (or model part) and gets the data
necessary for the presentation from the model by asking questions. It
may also update the model by sending appropriate messages. All these
questions and messages have to be in the terminology of the model, the
view will therefore have to know the semantics of the attributes of
the model it represents.

Controllers

A controller is the link between a user and the system. It provides
the user with input by arranging for relevant views to present
themselves in appropriate places on the screen. It provides means for
user output by presenting the user with menus or other means of giving
commands and data. The controller receives such user output,
translates it into the appropriate messages and pass these messages on
to one or more of the views.

点赞