49

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.

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

Malice
  • 3,809
  • 1
  • 34
  • 49
GordonM
  • 29,211
  • 15
  • 77
  • 126
  • 21
    Could you write a sentence or two about what the podcast says is wrong with MVC, so that readers here don't have to listen to it to understand the basic objections? I should be interested as well `:)` – halfer Aug 03 '12 at 16:48
  • The podcast link is broken beyond repair (nothing in the wayback machine). It has been replace by [this page](https://devzone.zend.com/1860/zendcon-sessions-episode-037-why-mvc-is-not-an-application-architecture/), but the link to the podcast is also broken there. However if you search for the talk "Why MVC is not an Application Architecture", you get this [video presentation](https://vimeo.com/40968850) by the same person. – icc97 Dec 15 '17 at 13:05

4 Answers4

104

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

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.

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

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

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

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

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

  • event driven architectures
  • n-Tier architecture

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

Ben Jackson
  • 78,375
  • 8
  • 86
  • 141
tereško
  • 56,151
  • 24
  • 92
  • 147
  • 1
    Thanks, these look interesting. Model2, especially. I'll research these and see which one looks like the best fit for what I'm aiming to do. – GordonM Oct 01 '11 at 20:37
  • `"To implement a classical MVC it would require for you to have persistent Models to begin with."`, what does this sentence infer? Persistent? Repetitive? Without any change? Does it infer `"model without side-effects"` or something totally different? – hhh Jul 31 '12 at 09:20
  • 7
    In classical MVC the view observes the model *(or structures from model layer, to be exact)*. When state of model changes, the view requests new information. With "persistent model" I meant that model layer exists all thought the usage of application. This is not possible in PHP because when you send the response, the instance from model layer are destroyed. This is a constraint of PHP's *Share-Nothing* architecture. The is no way for view to observe the model layer .. the is nothing left to observe. – tereško Aug 03 '12 at 16:42
  • Looks promising [n-Tier architecture](http://en.wikipedia.org/wiki/Multitier_architecture) – CoR Apr 24 '13 at 10:07
  • 2
    As a sidenote: There are (more or less) complicated solutions to enable persistent sessions in php, which in turn will allow us to create persistent models. This is the solution I am familiar with: http://us2.php.net/manual/en/intro.memcache.php – Philipp Aug 08 '13 at 21:22
  • (+1, for always explaining in details) @tereško - Sorry for popping up a 2 years old question, but as I was reading I got confused on the difference between a [front controller pattern](http://en.wikipedia.org/wiki/Front_Controller_pattern) and model2 MVC? – mamdouh alramadan Nov 14 '13 at 13:44
  • @mamdouhalramadan Sorry for popping up 3 years later (ha), but my stab at answering your question: The Front Controller is one design pattern, among many, typically used [within Model2 MVC](http://prasunejohn.blogspot.com/2013/07/architecture-and-control-flow-of-mvc2.html). However, Model2 MVC can have 10 controllers, and each controller can render multiple views. This is not using a Front Controller, but still removes controller-view pairing (classic MVC). [See here](http://ptgmedia.pearsoncmg.com/imprint_downloads/informit/chap2_0672324725.pdf). – Katrina Dec 27 '16 at 17:36
4

From my experience, the benefits you get from an MVC architecture far outweighs its costs and apparent overhead when developing for the 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.

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.

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.

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!

pcalcao
  • 15,237
  • 1
  • 40
  • 62
  • 1
    I'm all for compartmentalization, but as pointed out in the podcast, MVC seems like it's not the right approach. The "view" was meant to be an individual display element in a user interface, not the user interface as a whole. Furthermore in Zend in particular, the whole thing seems to be far too dependant on magic - it's hard to know where program flow control will jump next without manually tracing through it with XDebug or the like. That might just be an artefact of Zend's heavy use of singletons and registries though. – GordonM Oct 01 '11 at 20:34
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.

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.

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

jprofitt
  • 10,710
  • 4
  • 33
  • 45