Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
    
So, in conclusion: PHP frameworks implement a concept similar to the original MVC. I think it's best nailed here: stackoverflow.com/questions/1549857/simple-php-mvc-framework/… –  mario Oct 9 '10 at 6:29
    
I was surprised to read that "most PHP frameworks use Views as simply pages". In all the PHP frameworks I've used, a View can be anything, it's basically just an HTML template. So it could be a textbox, a sidebar, a navigation bar, a block of static text, or even a page layout. I can't think of any framework that doesn't let you embed Views within Views, letting you do pretty much anything as long as your actual business logic/processing is done in the Controller beforehand. –  Lotus Notes Jul 21 '11 at 17:03
    
The Model (not plural) is a layer. It is not one single file or class. It is a collection of domain objects, data mappers and services. Read this. –  Jimbo May 7 '13 at 14:10
1  
...SEO? "Search Engine Optimization"? –  Izkata May 7 '13 at 16:01
add comment

4 Answers

up vote 11 down vote accepted

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.

share|improve this answer
    
I have no quarrel with the abstract concept of MVC. It's my objection that PHP frameworks are dishonest about actually just implementing Passive-MVC. Even the "Model-View-Presenter" pattern is a more realistic description. But sure, terms must be bended when you apply them to a different domain. The original question; might the term bending make it a buzzword? –  mario Sep 25 '10 at 22:06
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
    
Sure, AJAX UI elements bring it closer, but that's objectively a workaround. And it still seems bending the definition. (Btw, I'm aware of Cappucino.org and other real toolkits, but was refereing to the gross of PHP frameworks.) –  mario Sep 11 '10 at 10:24
    
Wouldn't call it a workaround, you might as well count Qt and other frameworks as workarounds too then... There is only the data transfer overhead between server and client, and with the current connection speeds and latencies this isn't even much anymore. I don't see how it's bending the definition: The pattern isolates domain logic (the application logic for the user) from input and presentation (UI), permitting independent development, testing and maintenance of each. –  Tom Wijsman Sep 11 '10 at 10:43
1  
I see what you mean. If you interpret PHP as application server and AJAX as RPC mechanism between logic and UI, yes. I'd still call it a workaround on HTTP however. OTOH not sure if it's relevant to the MVC denomination. I think I'm actually objecting to the implication that only """MVC""" provides the responsive and interactive web UIs you describe. –  mario Sep 11 '10 at 10:51
add comment

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.

share|improve this answer
    
+1 But is it just a terminology advantage, or are there PHP frameworks that are close to a Java implementation. (And implicitely, are you talking about Java GUIs or Web/Struts?) –  mario Sep 11 '10 at 11:06
    
I don't know exactly but I'm using zend framework and I guess it's the same with other MVC frameworks: it's very important to know what to do in your model, view and controller and so the gap between the programming world and the internetscripting world is closed. Maybe the internetscripting age is over, and I would love to see that. It's too buggy. –  baklap Sep 11 '10 at 14:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.