I've been learning Zend and its MVC application structure for my new job, and found that working with it just bothered me for reasons I couldn't quite put my finger on. Then during the course of my studies I came across articles such as MVC: No Silver Bullet and this podcast on the topic of MVC and web applications. The guy in the podcast made a very good case against MVC as a web application architecture and nailed a lot of what was bugging me on the head.

我一直在为我的新工作学习Zend及其MVC应用程序结构,并发现使用它只是困扰我,原因是我无法完全理解。然后在我的学习过程中,我遇到了一些文章,如MVC:No Silver Bullet以及关于MVC和Web应用程序主题的播客。播客中的那个人对MVC作为一个Web应用程序架构提出了非常好的理由,并且钉了很多让我烦恼的东西。

However, the question remains, if MVC isn't really a good fit for web applications, what is?

但问题仍然存在,如果MVC不适合Web应用程序,那么它是什么?

4 个解决方案

#1


92

It all depends on your coding style. Here's the secret: It is impossible to write classical MVC in PHP.

这一切都取决于你的编码风格。这就是秘密:用PHP编写经典的MVC是不可能的。

Any framework which claims you can is lying to you. The reality is that frameworks themselves cannot even implement MVC -- your code can. But that's not as good a marketing pitch, I guess.

任何声称你可以使用的框架都在骗你。实际情况是框架本身甚至无法实现MVC - 您的代码可以。但我认为这不是一个好的营销宣传。

To implement a classical MVC it would require for you to have persistent Models to begin with. Additionally, Model should inform View about the changes (observer pattern), which too is impossible in your vanilla PHP page (you can do something close to classical MVC, if you use sockets, but that's impractical for real website).

要实现经典的MVC,您需要拥有持久性模型。另外,Model应告知View有关更改(观察者模式),这在您的vanilla PHP页面中也是不可能的(如果您使用套接字,您可以做一些接近经典MVC的事情,但这对于真实网站来说是不切实际的)。

In web development you actually have 4 other MVC-inspired solutions:

在Web开发中,您实际上还有其他4个受MVC启发的解决方案:

  • Model2 MVC: View is requesting data from the Model and then deciding how to render it and which templates to use. Controller is responsible for changing the state of both View and Model.

    Model2 MVC:View正在从Model请求数据,然后决定如何呈现它以及使用哪些模板。 Controller负责更改View和Model的状态。

  • MVVM: Controller is swapped out for a ViewModel, which is responsible for the translation between View's expectations and Models's logic. View requests data from controller, which translates the request so that Model can understand it.

    MVVM:Controller被换成ViewModel,ViewModel负责View的期望和Models的逻辑之间的转换。查看来自控制器的请求数据,该数据转换请求以便模型可以理解它。

    Most often you would use this when you have no control over either views or the model layer.

    当您无法控制任何视图或模型层时,通常会使用此方法。

  • MVP (what php frameworks call "MVC"): Presenter requests information from Model, collects it, modifies it, and passes it to the passive View.

    MVP(php框架称之为“MVC”):Presenter从Model请求信息,收集信息,修改信息并将其传递给被动视图。

    To explore this pattern, I would recommend for you begin with this publication. It will explain it in detail.

    为了探索这种模式,我建议您从本出版物开始。它会详细解释。

  • HMVC (or PAC): differs from Model2 with ability of a controller to execute sub-controllers. Each with own triad of M, V and C. You gain modularity and maintainability, but pay with some hit in performance.

    HMVC(或PAC):与Model2的不同之处在于控制器执行子控制器的能力。每个都有自己的M,V和C三元组。你获得了模块化和可维护性,但是在性能上有所收获。

Anyway. The bottom line is: you haven't really used MVC.

无论如何。底线是:你还没有真正使用过MVC。

But if you are sick of all the MVC-like structures, you can look into:

但如果您厌倦了所有类似MVC的结构,您可以查看:

  • event driven architectures
  • 事件驱动的体系结构
  • n-Tier architecture
  • n层架构

And then there is always the DCI paradigm, but it has some issues when applied to PHP (you cannot cast to a class in PHP .. not without ugly hacks).

然后总是存在DCI范例,但是当应用于PHP时它有一些问题(你不能在PHP中转换为类...而不是没有丑陋的黑客)。

更多相关文章

  1. 在Yii中获取当前控制器和操作ID
  2. 从模型到控制器并返回到模型的数据
  3. 保存在Java桌面应用程序应用程序和网站上使用的个人用户设置的最
  4. 有没有办法确定是由Web应用程序(php进程)执行数据库行更新,还是通过
  5. 如何在产品和类别应用程序树中将1个表连接到(2个不同的表作为一个
  6. 求问vs窗体应用程序用gridview连接mysql未能获取数据库对象的列
  7. Android应用程序与外部数据库之间的安全性
  8. 制作一个基本的angularjs应用程序对我不起作用,我也不知道为什么
  9. 将JSON ID键插入到ng-click指令中,然后将其传递到另一个控制器

随机推荐

  1. SmartRefreshLayout集成笔记,实现下拉刷新
  2. Android Camera进行拍照
  3. Android设置屏幕旋转后保存数据
  4. Android中的一些小知识点
  5. android 打开系统相册得到路径 上传图片
  6. android多线程数据存储 - ThreadLocal的
  7. Gradle for Android 系列:初识 Gradle 文
  8. Android 软键盘弹出时把原来布局顶上去的
  9. 求android大神指点
  10. android 关于webview缩放和适应屏幕的问