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应用程序,那么它是什么?
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的结构,您可以查看:
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中转换为类...而不是没有丑陋的黑客)。
4
From my experience, the benefits you get from an MVC architecture far outweighs its costs and apparent overhead when developing for the web.
根据我的经验,从MVC架构中获得的好处远远超过了它在开发Web时的成本和明显的开销。
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!
对不起文字的轻微墙!
0
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架构可能会有点过分。
0
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 World中工作,用于构建ASP.NET网站我没有看到使用MVC构建网站的其他方法。 Visual Studio中的Web应用程序项目仍在使用,但我不再喜欢了。通过Visual Studio的MVC项目非常易于使用和设置。您可以右键单击控制器方法并自动创建视图。在每个结构中都有好的和坏的但是开发人员可以使用满足他们需求的任何东西。
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2011/10/01/6f42b10c67fba85c680e0eed10b77d46.html。