Question

There's around a zillion "PHP frameworks". And most of them bill themselves as following the MVC pattern. While it's welcome to overcome osCommerce coding style (processing logic heavily intermixed with SQL and HTML), there are certainly simpler and easier to follow approaches to get a maintainable application design.

The original MVC concept was targetted at GUI applications. And for Gtk/Python it seems feasible to follow it accordingly. But PHP web apps don't operate on live Views (GUI elements) and a persistent Controller runtime. It's quite certainly a misnomer if it just describes the used code + directory grouping or class naming.

"MVC" seems to be used like a buzzword for PHP frameworks. And I've actually seen one or two mature PHP frameworks admit it, but redefining the phrase anyway to match interna.
So is it generally snake oil? Why isn't better terminology used, and a more sensible concept for maintainable PHP propagated?

Some elaborative reasoning

Why I suspect that PHP implementations don't follow the real MVC pattern:

Models: in theory, Models should be fat and contain business logic, and controllers should be thin handlers (input->output). In reality the PHP frameworks advocate shallow Models. CI and Symfony for example equate Model == ORM. Even HTTP input is handled by the controller, isn't treated as model.

Views: workarounds with AJAX discounted, there can't be Views on web pages. PHP frameworks still pump out pages. The interface still effectively follows the ordinary HTTP model, there's no advantage over non-MVC applications. (And lastly, none of the widespread php frameworks can factually output to GUI Views instead of HTML. I've seen a PHP library that can operate Gtk/Console/Web, but the frameworks don't.)

Controller: I'm unsure. Controllers probably don't need to be long-running and persistently active in the MVC model. In PHP framework context, they're however mostly request handlers. Not really something to get argumentative about, but it just feels slightly buzzwordish.

Would there be better descriptors? I've seen acronyms like PMVC or HMVC thrown around. Though descriptions get more ambigous there, maybe these would describe the current web frameworks less hokey?

Was it helpful?

Solution

I think you are looking at this in completely the wrong way. A GUI app and a web page are worlds apart so the exact same definition of MVC will never work for both. MVC is more about the ideal: separating certain parts of the app like display and logic.

In PHP (or the web in general), a View is the web page itself: the HTML output. It's not "live" as per your definition, but you simply click links to go back to the controller (i.e. another page request).

The Controller and Model is where things do differ, like you explained. In PHP the model tends to be the data layer, interacting with the database and so on. But it is still modelling the situation, and the controller still controls the application flow, if only once per page load.

So the name "Model-View-Controller" is perfectly logical, albeit a different implementation in GUI apps vs web apps.

OTHER TIPS

As I'm unaware of the PHP frameworks this is seen from a low-level language view.

Models:

in theory, Models should be fat and contain business logic

That's completely up to do, I don't see what PHP has to do with this...

Models are data classes in PHP which could probably communicate with the database,
then you could also send the same model or a partial model in JSON format to the client.

I wouldn't say business logic, it's more like data logic (validation, database interaction, import/export, ...).

and controllers should be thin handlers (input->output)

Your Controller classes interact with the Model classes, they are indeed thin.

Based on the output, do some things with the Models... And return a ModelView to the client...

In reality the PHP frameworks advocate shallow Models. CI and Symfony for example equate Model == ORM. Even HTTP input is handled by the controller, isn't treated as model.

I'm not really aware of those PHP frameworks...

But HTTP input should be handled before it reaches the controller,
you can easily create a class that turns GET and POST data into good routing and parameters.

This is exactly what happens in ASP.NET MVC 2 and there is nothing wrong with it,
I don't know how this would happen with PHP but I guess it would be closely related.

You could even easily turn the GET and POST data into a model, the model maybe could contain constructor logic for that. Or some separate classes could be added for that purpose.


Views:

workarounds with AJAX discounted, there can't be Views on web pages. PHP frameworks still pump out pages.

I don't see why it couldn't, the only difference is the protocol and PHP can return JSON, etc...

A page is your view and it can request and update through AJAX + JSON.
Again, I'm not really aware of those PHP frameworks but in ASP.NET MVC 2 it works that way.

The interface still effectively follows the ordinary HTTP model, there's no advantage over non-MVC applications. (And lastly, none of the widespread php frameworks can factually output to GUI Views instead of HTML. I've seen a PHP library that can operate Gtk/Console/Web, but the frameworks don't.)

The only advantage you get (and that's the same with normal applications) is the separation into Model (Data) + View (GUI) + Controller (Logic). Similar, you won't see a C++ framework that can factually output to HTML or JSON instead of GUI Views.


Controller:

I'm unsure. Controllers probably don't need to be long-running and persistently active in the MVC model. In PHP framework context, they're however mostly request handlers. Not really something to get argumentative about, but it just feels slightly buzzwordish.

MVC is a software architecture/pattern, where the Controller runs and for how long doesn't mather.

But PHP web apps don't operate on live Views (GUI elements) and a persistent Controller runtime.

No, they sure do!

Think of AJAX applications, then the view asks something to the controller and gets a partial view back,
this view or data is then filled in somewhere in the page and thus live updated.

The Controller is also persistent because you can use cookies/sessions.

"MVC" seems to be used like a buzzword for PHP frameworks.

MVC is a Software Architecture, some frameworks might use it as a buzz, but others do it properly...
See a list of some frameworks on Wikipedia.

is MVC just the SEO of php programming?

MVC and SEO are two things apart, but yes... MVC is getting more popular.

In my opinion using MVC in php brings programmers to the web. It's easier to get from for example Java to PHP when you know how to work with MVC.

Licensed under: CC-BY-SA with attribution
scroll top