基本 MVC (PHP) 结构
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11689155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Basic MVC (PHP) Structure
提问by Craig Wilson
I have the following data flow for a simple login form.
我有一个简单的登录表单的以下数据流。
User access controller PHP file. Controller includes model.php and view.php
用户访问控制器 PHP 文件。控制器包括 model.php 和 view.php
User submits form, controller sends POST data to model methods, and gets a result back.
用户提交表单,控制器将 POST 数据发送到模型方法,并返回结果。
User is logged in, and forwarded to a different view (login success message) by the controller.
用户已登录,并由控制器转发到不同的视图(登录成功消息)。
Currently my views are static HTML (no PHP), so here is my question. What is the correct way to then pass the user a welcome message, e.g "Hello, Craig!"?
目前我的观点是静态 HTML(没有 PHP),所以这是我的问题。然后向用户传递欢迎消息的正确方法是什么,例如“你好,克雷格!”?
Is the view allowed PHP snippets, e.g
视图是否允许 PHP 片段,例如
<?php echo $username; ?>
since the model is loaded before it in the controller file?
因为模型在控制器文件中是在它之前加载的?
Thanks!
谢谢!
Edit:Is it better practice then to allow the view to access specific class methods e.g
编辑:然后允许视图访问特定的类方法是更好的做法,例如
<?php $user->getUsername(); ?>
as opposed to just variables?
而不仅仅是变量?
Based on other answers, I have found a very useful article, which you may also be interested in.
根据其他答案,我找到了一篇非常有用的文章,您可能也感兴趣。
http://www.nathandavison.com/posts/view/7/custom-php-mvc-tutorial-part-5-views
http://www.nathandavison.com/posts/view/7/custom-php-mvc-tutorial-part-5-views
采纳答案by Jeff Lambert
You can really put anything in a view that you'd like, but to better adhere to the MVC way of doing things you should restrict PHP in the view to simple echos or prints (possibly really small loops as well, although even those can be pre-calculated in the controller/model). Since that is the only way to get dynamic content, it would be a little silly to say that they are not allowed.
你真的可以把任何东西放在你喜欢的视图中,但为了更好地坚持 MVC 做事方式,你应该将视图中的 PHP 限制为简单的echos 或prints(也可能是非常小的循环,尽管即使是那些也可以在控制器/模型中预先计算)。由于这是获取动态内容的唯一方法,所以说它们不被允许有点愚蠢。
The idea of the view is to let it have a more HTML look-and-feel, so that front-end developers or people who don't know PHP can easily be able to work with the file without getting confused.
视图的想法是让它具有更多的 HTML 外观,以便前端开发人员或不了解 PHP 的人可以轻松地使用该文件而不会感到困惑。
Update
更新
To learn more about MVC in general, you can see any of these (there's a ton of tutorials out there):
要了解有关 MVC 的更多信息,您可以查看其中任何一个(那里有大量教程):
http://blog.iandavis.com/2008/12/09/what-are-the-benefits-of-mvc/
http://blog.iandavis.com/2008/12/09/what-are-the-benefits-of-mvc/
http://php-html.net/tutorials/model-view-controller-in-php/
http://php-html.net/tutorials/model-view-controller-in-php/
http://www.tonymarston.net/php-mysql/model-view-controller.html
http://www.tonymarston.net/php-mysql/model-view-controller.html
To see concrete examples of PHP using MVC, I suggest downloading some of the more prevelant frameworks (such as CodeIgniter, Symfonyor Drupal) and just looking through the code. Try to figure out how it works and then recreate the functionality for a simple article-based system.
要查看使用 MVC 的 PHP 的具体示例,我建议下载一些更流行的框架(例如CodeIgniter、Symfony或Drupal)并查看代码。尝试弄清楚它是如何工作的,然后为一个简单的基于文章的系统重新创建功能。
回答by tere?ko
Here are few things you must consider:
您必须考虑以下几点:
- You cannot do classical MVC in PHP. Instead we have MVC-inspiredpatterns
- There exists 1:1 relation between view and controller instances, when implemented for web
- Model in MVC is not a class. It is a layer, that contains a lot of different classes
- View is not a dumb template, but an instance of class, which deals with presentation logic
- 你不能在 PHP 中做经典的 MVC。相反,我们有受MVC 启发的模式
- 当为 web 实现时,视图和控制器实例之间存在 1:1 的关系
- MVC 中的模型不是一个类。它是一个层,包含许多不同的类
- View 不是一个愚蠢的模板,而是一个类的实例,它处理表现逻辑
View in Web-based MVC
在基于 Web 的 MVC 中查看
As stated above, viewsin MVC and MVC-inspired patterns are responsible for presentation logic. That encompass things like showing error messages and pagination. To do this, each view can handle several templates.
如上所述,MVC 和 MVC-inspired 模式中的视图负责表示逻辑。这包括显示错误消息和分页等内容。为此,每个视图可以处理多个模板。
View receives information from the model layer, and acts accordingly. The way how the information from model layer ends up in views is one of most significant differences in MVC-ish patterns:
View 从模型层接收信息,并相应地采取行动。来自模型层的信息最终出现在视图中的方式是 MVC 模式中最显着的差异之一:
classical MVCpattern
Structures from model layer send the information to view, when state of model has been altered. This is done via observer pattern.
Model2 MVCand HMVCpatterns
View has direct access to the model layer and is able to request information from it. This is the closest to the original pattern.
MVVMand MVPpatterns
View receives information through controller, which has in turn requested it from model layer. The further difference in patterns stems from what the do with data before passing it to view.
经典的MVC模式
当模型状态发生改变时,模型层的结构将信息发送到视图。这是通过观察者模式完成的。
Model2 MVC和HMVC模式
视图可以直接访问模型层,并能够从中请求信息。这是最接近原始模式的。
MVVM和MVP模式
视图通过控制器接收信息,控制器又从模型层请求它。模式的进一步差异源于在将数据传递给视图之前如何处理数据。
What you seem to have now is actually just a template. Similar to one, that is described in this article. You end up with a structure, that has no place to contain the presentation logic. In long-run this will cause the presentation logic to be pushed into controller.
你现在似乎拥有的实际上只是一个模板。与本文中描述的类似。你最终得到一个结构,它没有地方包含表示逻辑。从长远来看,这将导致表示逻辑被推送到控制器中。
So what about that "welcome" message ?
那么那个“欢迎”信息呢?
To show the welcome message, your view should request from model layer the name of current user. If the model layer returns some sort of error state, view pick the error message template and inserts into the layout.
要显示欢迎消息,您的视图应该从模型层请求当前用户的名称。如果模型层返回某种错误状态,则视图选择错误消息模板并插入到布局中。
In case if name of the user was retrieved from model layer without problems, view pick the template which would contain the greeting, sets the value in the template and renders it.
如果从模型层检索到用户名没有问题,则视图选择包含问候语的模板,在模板中设置值并呈现它。
In what order parts should be loaded ?
零件应按什么顺序装载?
The idea, that controller should initialize model and view, comes from very primitive interpretation of MVC for web. Pattern know as page controller, which tried to graft MVC directly on static web pages.
控制器应该初始化模型和视图的想法来自对 Web MVC 的非常原始的解释。模式称为页面控制器,它试图直接在静态网页上嫁接MVC。
In my opinion, this should be the order:
在我看来,顺序应该是:
Model
You initialize the structure, through which you will deal with model layer. It most likely would be some sort of service factory, which would let you build things like
Authenticationservice for logins andLibraryservice for handling documents. Things like that. I wrote a bit long'ish comment on model layer's structureearlier. You might find it useful.View
You create a view instance based on information, that you collected from routing mechanism. If you are implementing Model2or HMVC, then your view will require an instance of Service Factoryin the constructor.
If you are implementing MVVMor MVP, then view's constructor has no special requirements.
Controller
This is the last structure, which you create, because controller is responsible for sending commands to both view and model layer, which then change then change the state of both. Therefore controller should expect to receive both view and service factory in the constructor.
模型
您初始化结构,通过它您将处理模型层。它很可能是某种服务工厂,它可以让您构建诸如
Authentication用于登录的Library服务和用于处理文档的服务之类的东西。像这样的东西。我之前对模型层的结构写了一点长篇大论。你可能会发现它很有用。看法
您根据从路由机制收集的信息创建视图实例。如果您正在实现Model2或HMVC,那么您的视图将需要构造函数中的Service Factory实例。
如果您正在实现MVVM或MVP,则视图的构造函数没有特殊要求。
控制器
这是您创建的最后一个结构,因为控制器负责向视图层和模型层发送命令,然后更改然后更改两者的状态。因此控制器应该期望在构造函数中同时接收视图和服务工厂。
After basic elements of MVC have been initialized, you call a method on the controller, and render current view.
在 MVC 的基本元素被初始化之后,你在控制器上调用一个方法,并渲染当前视图。
Just keep in mind that this is very simplified description.
请记住,这是非常简化的描述。

