简体   繁体   English

Zend View Helper中的数据库连接

[英]Database Connection in Zend View Helper

Is it a good practice to use database mappers in Zend View Helper? 在Zend View Helper中使用数据库映射器是一种好习惯吗? Because in my case this helper is making me a box of <div> that keeps changing in real time and is to be shown in all the views there are in my application. 因为在我的情况下,此帮助程序使我成为一个<div>框,该框一直实时更改,并显示在应用程序中的所有视图中。 I can not possibly give that object loading it from the database in controllers and assign it to view everytime. 我不可能给那个对象从控制器中的数据库中加载它并分配给它每次查看。

It would be really helpful if someone could tell good programming practices to follow when working with zend view helpers like: 如果有人在使用zend view helper时能告诉良好的编程习惯,这将非常有帮助:

  • If it is OK to assign something to the view in the View Helpers by $this->view->variable = ... ; 如果可以,可以通过$this->view->variable = ... ;在View Helpers中为视图分配某些内容$this->view->variable = ... ;
  • If it is OK to create and use models in View Helpers. 如果可以,可以在View Helpers中创建和使用模型。
  • If it is OK to use methods available to Zend_View inside a View Helper like by doing $this->view->baseUrl('...'); 如果可以在View Helper中使用Zend_View可用的方法,例如通过执行$this->view->baseUrl('...');

Your second and third bullet points seem correct to me, as long as you don't do any logical stuff in your model from your views. 只要您不从模型的角度对模型进行任何逻辑上的选择,第二点和第三点对我来说都是正确的。 The link between models and views must be read-only. 模型和视图之间的链接必须是只读的。

Concerning your first point, you don't need to assign anything to the view, you view helper should return your HTML output directly to the view. 关于第一点,您无需为视图分配任何内容,视图帮助程序应将HTML输出直接return给视图。

About your first question, you could create View Helper that is specialized in this task, so you can use it as a simple proxy between your view helpers and mappers. 关于第一个问题,您可以创建专门用于此任务的View Helper,因此可以将其用作View Helper和Mappers之间的简单代理。 One view helper will allow you to access to any mapper, and others view helpers can use this view helper to get a mapper. 一个视图助手将允许您访问任何映射器,而其他视图助手可以使用此视图助手来获取映射器。

Let's see what Trygve Reenskaug thinks about MVC: 让我们看看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. 控制器接收此类用户输出,将其转换为适当的消息,然后将这些消息传递到一个或多个视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM