php 架构比 MVC 更适合 Web 应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7621832/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:03:23  来源:igfitidea点击:

Architecture more suitable for web apps than MVC?

phpmodel-view-controllerweb-applicationsarchitecture

提问by GordonM

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 Bulletand this podcaston 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 应用程序,那什么是合适的?

回答by tere?ko

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 有关更改(观察者模式),这在您的普通 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.

  • 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.

    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.

    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.

  • Model2 MVC:视图从模型请求数据,然后决定如何呈现它以及使用哪些模板。控制器负责改变视图和模型的状态。

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

    大多数情况下,当您无法控制视图或模型层时,您会使用它。

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

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

  • 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 DCIparadigm, but it has some issues when applied to PHP (you cannot cast to a class in PHP .. not without ugly hacks).

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

回答by pcalcao

From my experience, the benefits you get from an MVC architecture far outweighs its costs and apparent overhead when developing for the web.

根据我的经验,在为 Web 开发时,您从 MVC 架构中获得的好处远远超过其成本和明显的开销。

For someone starting out with a complex MVC framework, it can be a little daunting to make the extra effort of separating the three layers, and getting a good feel as to what belongs where (some things are obvious, others can be quite border-line and tend to be good topics of discussion). I think this cost pays for itself in the long run, especially if you're expecting your application to grow or to be maintained over a reasonable period of time.

对于刚开始使用复杂 MVC 框架的人来说,付出额外的努力来分离三层,并很好地了解什么属于哪里可能有点令人生畏(有些事情是显而易见的,有些事情可能非常边界线)并且往往是很好的讨论话题)。我认为从长远来看,这种成本是物有所值的,特别是如果您希望应用程序在合理的时间段内增长或得到维护。

I've had situations where the cost of creating a new API to allow other clients to connect to an existing web application was extremely low, due to good separation of the layers: the business logic wasn't at all connected to the presentation, so it was cake.

我遇到过这样的情况,由于层的良好分离,创建新 API 以允许其他客户端连接到现有 Web 应用程序的成本极低:业务逻辑根本没有连接到表示,所以这是蛋糕。

In the current MVC framework eco-system I believe your mileage may vary greatly, since the principles are common, but there are alot of differences between, for instance, Zend, Django, RoR and SpringMVC.

在目前的MVC框架生态系统中,相信大家的里程可能会有很大的不同,因为原理是通用的,但是在Zend、Django、RoR和SpringMVC等之间存在很多差异。

If there are truly other good alternatives to this paradigm out there... I'm quite interested in the answers!

如果这个范式真的有其他好的替代品......我对答案很感兴趣!

Sorry for the slight wall of text!

对不起,文字的轻微墙!

回答by jprofitt

I think it would depend on what you're trying to do, personally. Magenta uses MVC pretty successfully, and it makes it fairly easy to add new functionality or modify existing.

我认为这取决于你个人想要做什么。Magenta 非常成功地使用 MVC,它使得添加新功能或修改现有功能变得相当容易。

Of course if you're trying to make something fairly simple, going with an MVC architecture could be overkill.

当然,如果您想让一些东西变得相当简单,那么使用 MVC 架构可能有点过头了。

回答by vou xiong

It's all preference. I have worked with old structures like XTemplates and Smarty and have now moved on to Codeigniter and Kohona. I like them very much and they work very well for everything I do on the web. For phone applications I can set up controllers for the functions that are needed to do it's data pulls as well. Working both in the Linux world and Windows World, for building ASP.NET Web Sites I don't see other way of building websites beside using MVC. Web Applications Projects in Visual Studio are still used but I prefer not to anymore. MVC Projects via Visual Studio is so easy to use and set up. You can right click on your controller methods and create views automatically. In every structure there is a good and bad but it's up to the developer to use whatever meets their needs.

都是偏好。我曾经使用过像 XTemplates 和 Smarty 这样的旧结构,现在已经转向 Codeigniter 和 Kohona。我非常喜欢它们,它们适用于我在网络上所做的一切。对于电话应用程序,我也可以为完成数据提取所需的功能设置控制器。在 Linux 世界和 Windows 世界中工作,用于构建 ASP.NET 网站我看不到除了使用 MVC 之外的其他构建网站的方法。Visual Studio 中的 Web 应用程序项目仍在使用,但我不想再使用了。通过 Visual Studio 的 MVC 项目非常易于使用和设置。您可以右键单击控制器方法并自动创建视图。在每个结构中都有好坏之分,但开发人员可以使用任何满足他们需求的方法。