了解PHP中的MVC视图 [英] Understanding MVC Views in PHP

查看:87
本文介绍了了解PHP中的MVC视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不得不在MVC中理解Views的概念,他们是,根据我已经阅读,在管理演示文稿的层,但我已经阅读的许多材料似乎是不同的问题在这一个从 PHP Master.com



视图是一个类,它返回一些HTML代码的函数,其余的HTML是什么?是否应该放在访问此View代码的独立的.html页面中?



在本文中,从 require()或任何类似第一个教程中的实例化。

注意: MVC和MVC风格模式是高级结构。它们用于普通面向对象的代码库中(遵循 SOLID 和其他准则)代码开始变得难以管理。通过引入此模式,您将施加额外的约束,然后允许您包含非常复杂的应用程序。 MVC 是指hello world应用程序。




...



MVC和MVC设计模式背后的核心理念是分离关注。所述分离是双重的:




  • 模型层与表示层分离:

  • 与控制器分开



模型图层(不是类或对象)将包含几组结构,每个处理作为业务逻辑的不同方面。主要部分是:




  • 域对象:验证,业务规则

  • 存储抽象:来自域对象的数据持久化和缓存

  • 服务:应用程序逻辑



此外,在存储库中可能会混用,工作单元等。



演示层主要由视图和控制器组成。但是他们都利用服务与模型层交互。服务为控制器提供了改变模型层的状态和视图以基于该新状态收集信息的方式。



在web的上下文中,视图和控制器形成一个松散的对,因为web应用程序展示的请求 - 响应性质。



应该注意,虽然控制器可以当前视图直接,更常见的是这些更改是通过模型实现的。改变视图直接的一个原因是,例如,而不是XML,你需要用JSON响应。



虽然也可以说,可以简单地为每种输出格式实例化不同的视图,并利用多态性。



什么不是视图?



有一个普遍的误解,视图是简单美化的模板文件。这个错误在RubyOnRails原型框架发布后非常受欢迎。



视图不是模板。如果你使用它们,你打破了MVC和MVC启发模式背后的核心原则。



如果你假设模板是视图,它对你的建筑。在视图中没有UI逻辑的地方,因此你在控制器或模型层中推送UI逻辑。通常的选择是控制器,因为大多数人都明白UI逻辑在模型层没有地方。



本质上,这导致视图和控制器的合并。



视图是什么?



视图的职责是处理UI逻辑。在web的上下文中,视图的目标是产生对用户的响应(其中,btw是浏览器而不是人)



在技术上,可以创建客户端视图,用户web套接字来观察模型层,不可能实现。特别是在PHP环境中。


要创建此响应视图从模型层获取信息,通过将数据分发到模板和呈现或者有时简单地发送HTTP位置头来组合响应。


使用Post/Redirect/Get ,重定向部分由视图执行,而不是控制器,通常人们倾向于执行。 / sub>







h3>

最近我喜欢使用以下方法与MVC交互:

  /服务的工厂被注入构造函数
$ controller-> {$ method。$ command}($ request);
$ view-> {$ command}();
$ view-> respond();

$方法是当前的* REQUEST_METHOD *,已经调整过伪类似REST的API,而 $ command 是人们通常所说的action。控制器具有用于 GET POST (另一个)请求的单独例程。这有助于避免在每个动作中有如果



在视图中我调用两个方法。首先是动态调用收集数据。第二个目的是创建某种类型的响应。


警告:我怀疑此设置包含 SRP 违规。




DRY是什么?



正如你可能已经注意到的,将视图作为实例存在一个小问题。你最终会得到重复的代码段。例如:菜单或分页。



让我们看看分页..分页包含逻辑,但这个逻辑与模型层无关。模型没有页面的概念。相反,这一位逻辑将驻留在表示层。但是,如果你的每个视图都包含或继承分页,那么它将明显违反SRP(实际上还有其他一些原则)。



为了避免这个问题,你可以(和should,imho)在您的视图中引入表示对象



注意:Fowler称之为演示模型整个什么是模型的混乱。因此,我建议改为称为展示对象。


演示对象处理重复的逻辑。这使得视图更轻,并且在一些方面开始反映来自模型层的服务的结构。



表示对象和模板之间的交互

a>变得类似于域对象与数据映射器之间的交互。







PS 我自己仍然在努力想办法如何最好地处理视图。这个帖子不是一个答案,更像是我目前理解的快照。



I have to seem problems grasping the concept of Views in MVC, they are, according to what I've read, the layer that manages the presentation in the aplication, but many of the material I've been reading seem to be different regarding this matter in this one from PHP Master.com.

The View is a class with functions that return some HTML code, where is the rest of my HTML? should it be put in independent .html pages that access this View code?

In this article, from php-html.net the View is a simple HTML file with a .php extension, but how are they accessing that data? I see no require() or anything like the instantiations in the first tutorial.

解决方案

Note: the MVC and MVC-inspired patterns are advanced constructs. They are meant to be used in codebases where ordinary object-oriented (that follows SOLID and other guidelines) code starts to become unmanageable. By introducing this pattern you would impose additional constraints, which then lets you to contain very complex applications. MVC is not meant for "hello world" apps.

Let's start from the beginning ...

The core idea behind MVC and MVC-inspired design patterns is Separation of Concerns. Said separation is two-fold:

  • model layer is separate from presentation layer:
  • views are separated from controllers

Model layer (not "class" or "object") would contain several groups of structures, each dealing with as different aspect of business logic. The major parts would be:

  • domain objects: validation, business rules
  • storage abstraction: persistence and caching of data from domain objects
  • services: application logic

Also there might be mixed in repositories, units of work and others.

Presentation layer mostly consists of views and controllers. But they both utilize services to interact with the model layer. Services provide the way for controllers to change the state of model layer and for the views to gather information based on that new state.

In context of web the views and controllers form a loose pair, because of the request-response nature that web applications exhibit.

It should be noted that although controllers can alter the state of the current view directly, it's more common that these changes are effected through the model. One reason to alter the view directly is, for example, when instead of XML you need to respond with JSON.

Though it also could be argued that one could simple instantiate a different view for each output format and take advantage of polymorphism.

What is not view?

There is a widespread misconception that views are simply glorified template file. This mistake became extremely popular after release of RubyOnRails prototyping framework.

Views are not templates. If you use them as such, you break the core principle behind MVC and MVC-inspired patterns.

If you pretend that templates are views, it has an enormous impact on your architecture. There is no place for UI logic in the view, therefore you push the UI logic either in controller or model layer. The usual choice is "controller", because most of people understand that UI logic has no place in model layer.

Essentially, this causes a merger of views and controllers.

What is view doing?

The responsibility of the view is to deal with UI logic. In context of web the goal for view is to produce a response to the user (which, btw, is the browser not the human).

Technically it would be possible to create client side views, that user web sockets to observe model layer, but in practice it's virtually impossible to implement. Especially not in PHP environment.

To create this response view acquires information from model layer and, based on gathered data, either assembles response by distributing data to templates and rendering or sometimes simple sending a HTTP location header.

When using Post/Redirect/Get, the redirect part is performed by the view, not the controller as often people tend to do.


Highly subjective bit:

Lately I have preferred to interact with MVC using following approach:

  // the factory for services was injected in constructors
  $controller->{ $method.$command }($request);
  $view->{ $command }();
  $view->respond();

The $method is the current *REQUEST_METHOD*, that has been adjusted fake a REST-like API, and the $command is what people usually call "action". The controller has separate routines for GET and POST (an other) requests. This helps to avoid having same if in every "action".

On the view I call two methods. First is a dynamic call to gather the data. And second aims to create some type of response.

Warning:I suspect that this setup contains an SRP violation. Adopting it as your own might be a bad idea.

What about DRY?

As you might have noticed already, there is a slight problem with having views as instances. You would end up with repeating pieces of code. For example: menu or pagination.

Lets look at pagination .. The pagination contains logic, but this logic is not related to the model layer. The model has no concept of "page". Instead this bit of logic would reside in the presentation layer. But if each of your views contains or inherits pagination, then it would be a clear violation of SRP (and actually several other principles too).

To avoid this issue you can (and should, imho) introduce presentation objects in your views.

Note: while Fowler calls them "presentation models", I think that name just adds to the whole 'what is model' confusion. Therefore I would recommend to call them "presentation objects" instead.

The presentation objects deal with repeated pieces of logic. This makes the views much "lighter", and in some aspects starts to mirror the structure of services from the model layer.

The interaction between presentation objects and templates becomes similar to the interaction between domain objects and data mappers.


P.S. I myself am still struggling with figuring out a way how best to deal with views. This post is less of an answer and more like a snapshot of my current understanding.

这篇关于了解PHP中的MVC视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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