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.

I have been trying to wrap my head around MVC pattern for about 6 months.I've read tons of articles, Q&A, and blog posts about MVC and HMVC patterns, but I just simply don't get it 100%.

I tried learning MVC by exercising one of widely used PHP MVC framework, CodeIgniter. I've implemented few internal websites for my company with it. And STILL, I don't 100% get it. I believe one of the best ways to learn something is to follow strictly defined rules.

So my question is: What is the most strict PHP framework in terms of how MVC pattern is implemented? One that defines how I use models, controllers, and views so that I can completely get what MVC is?

share|improve this question

closed as off-topic by gnat, Martijn Pieters, GlenH7, Dan Pichelman, Bart van Ingen Schenau Mar 19 at 12:51

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions asking us to recommend a tool, library or favorite off-site resource are off-topic for Programmers as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." – gnat, Martijn Pieters, GlenH7, Dan Pichelman, Bart van Ingen Schenau
If this question can be reworded to fit the rules in the help center, please edit the question.

5 Answers 5

up vote 10 down vote accepted

Short answer

There is no such thing.

Longer version:

Frameworks do not implement MVC or MVC-inspired design pattern. Your application does.

MVC is not some magic sauce, that you can add to your application by dumping it in a framework. Instead, you would actually have to learn and understand said pattern (and the accompanying principles and practices, like SOLID, LoD, SoC). Only then you can try to use it within your choice of a framework.

If a framework advertises that it "has MVC", then it is completely utter dren. In that situation, it is used to "sell" the framework to the noobs that care more about the latest hype then about application design and coding practices.

The goal of a framework would be to provide you collection of tools, that, when used as whole, provide you with an altered/improved development environment. They deal with such things as routing, autoloading, low level abstractions for storage (no, I am not talking about active record anti-pattern) and the rest of thing that let save you time in boh development and maintenance.

In conclusion.

There are no MVC frameworks in PHP. All of those who claim to me such are actually among the worst ones. Namely - codeigniter, cakephp and yii. Avoid them at all costs (except if you get paid really well for it).

If you need to use a framework, currently your best options would be latest version of Symfony 2.x, Zend Framework 2.x or Laravel 4.x. These ones will not implement MVC for you, but instead will do the least harm for you own application architecture.

share|improve this answer
5  
-1 Clearly some frameworks are more suited to MVC than others. Usually it's the ones that include models, view and controllers. –  Rein Henrichs Apr 17 '13 at 22:11
6  
Model is not a class. it is an application layer. If you have a framework which has "models", then it is a clear indication that is not even remotely related to MVC design patter or concepts of which MVC is built on. –  teresko Apr 17 '13 at 22:14
5  
... it's an application layer that needs to be reified in some way. In an OO language that typically means using objects, which means using classes. In any event, I'm not interested in engaging with someone so openly rude and hostile towards the ideas of others so I'll leave this alone. –  Rein Henrichs Apr 17 '13 at 22:16
6  
-1 The OP was asking for a framework that strictly enforced the use of the MVC pattern in the application using the framework. Your answer was about "the best" framework and the philosophy about frameworks in general and did not answer the OP's question. –  Jason Holland Apr 17 '13 at 23:54
3  
+1 I have come to essentially the same conclusion over the years. Cake & co are the worst offenders; you may be able to get something MVCish out of them if you hammer on them long enough, but they really really aren't geared for actual MVC design. The best choice indeed are frameworks which are just a loose collection of components and let you implement your own application using their generalized tools. –  deceze Apr 18 '13 at 6:21

I can only speak to Cake, and I have nothing good to say about it in terms of MVC. They do not do MVC right. Codeigniter is very much in the same vein. It is not at all weird that you didn't "get" MVC even after having used it for a while.

MVC is simply about properly separating three distinct components of your application's logic: the core of the app, the presentation and the glue necessary to make both work in a real world context.

The core of your app, which contains all the business logic, database interaction, services, beeps and boops that make your app "your app" are the model. The model is not one specific thing in one specific shape, it is whatever is necessary to make your app work the way it does. You only have one "model", which is your app.

The View is used to output in some way what your model does. It's the user interface. It is whatever is necessary to show useful information to the user. This may be a website, it may be a command line interface, it may be a native desktop GUI. You may use all three in your app.

The Controller is simply whatever is left to make that work, mostly the thing that takes user input and directs it to the appropriate place. You may have several different kinds of controllers for different kinds of context, e.g. one that can handle incoming HTTP requests, one that handles command line input and one that is hooked up to GUI events.

What particular shape these individual parts take depends on your app entirely. All three can be sort of their own mini-applications. Any template-prefab framework "model" is made for one generalized case to help you get something up and running quickly; it often is not the optimal form your model should take. You'll have to come up with your own structure for your app, whatever is best suited to build it. Look into OOP principles, SOLID, dependency injection etc. and build your core model according to these guidelines. Then wrap views and controllers around it as necessary.

The point of this separation is simply to make the app maintainable and extendable. The model does not contain anything that is specific to any specific form of input or output. For example, it does not contain any format-specific text (like error messages in HTML format). It does not assume any particular form of input (like HTTP requests). The View conversely does not contain any business logic, its job is just to output. And the controller does not contain any business logic either, its job is only to "input". The reason is simply that both the controller and view are interchangeable, but your app is not.

If you want to use a framework for this, use one that is modular and lets you do whatever you need to do. Zend, Symfony, Laravel and similar component based pick-and-choose frameworks are best suited for this.

share|improve this answer
    
Thanks for clearing up some doubts! I sort of got what you mean when I read this line. both the controller and view are interchangeable, but your app is not. One question though: In any framework folder structure, there is a folder called 'models'. If 'my app' itself is the 'model', what should they, ones in this 'models' folder, really be called? –  kidonchu Apr 18 '13 at 18:35
    
One more question...is this tutorial considered to be similar to your understanding of MVC pattern? It seems they have somethings in common, but I am not sure; especially, admin's comment on having all logics in one model with many entity classes, this link. –  kidonchu Apr 18 '13 at 21:55
    
I've just skimmed that tutorial, but it seems more in line with "real" MVC. The model seems a little thin, but hopefully only because it's just an example. I do name my model folder "model", but within it I create a number of subfolders; typically I at least distinguish between "primitves" (like Book), services ("actions", like collateBookCollection) and storage (saveBookToDatabase). What you need to take care is that you don't put too much code into the controller, which is something Cake & co. get completely wrong. Make your model really fat instead. –  deceze Apr 18 '13 at 23:24
    
I name mine domain :p –  Esailija Apr 19 '13 at 5:35

Such a framework can not possibly exist for a number of reasons.

First of all, the Model in the MVC pattern is the part that makes your application your application. If a framework were to define strictly what a model looks like, then that framework would only be usable for one or at most a handful of applications. This effectively stops it from being a framework.

The second problem is that there is no universal consensus on what a proper application of the MVC pattern looks like. For example, some will say that the Controller must provide the View with the data it needs from the Model, while others will say that the View should retrieve the information it needs by itself and the Controller should just make sure that the View knows where to find the Model.
Another example, more specific to web applications, is the amount of processing that the View can do. For some, the View should consist exclusively of HTML files where some place holders get replaces with the actual content (usually retrieved by the Controller from the Model), while others are perfectly comfortable with letting the View do UI-related processing, such as internationalisation of the presented content.

share|improve this answer
1  
I see. I think I once saw this quote "If you ask 'what MVC is' to ten different people, you will get ten different answers". I guess that's what your second point means. –  kidonchu Apr 18 '13 at 18:14
    
@kidonchu: That is part of it, although you should also not be surprised to get 11 different answers from those 10 people ;-) –  Bart van Ingen Schenau Apr 18 '13 at 18:32

I understand this post is year old, which is ironically the reason I am posting this response. First off, some of the people who answered this question are right. You're never really going to find a PHP Framework that come "MVC Ready" right out of the box...necessarily. Seeing as the framework is supposed to be a foundation where Dev's can build upon it, following MVC. However, in the last year since this has been posted, some of the PHP frameworks out there have sure come a long way.

I messed around with CakePHP for a bit, even to this day it is a horrible, messy setup of directories and structure, there is no clear distinction between logic, it has quite poorly commented code, and everything is kind of crammed together in one bundled mess. That is not a rant on CakePHP mind you, that is simple truth.

Zend, is excellent. They provide excellent documentation & code commenting, have a very friendly community, and provide a very nice framework for developers that are novice, and intermediate alike. They have come a long way since the OP posted this.

And to the people saying that there is no such framework to answer the OP's question, they are right as I said above. However, they are also wrong as well. Zend, Laravel and CodeIgniter are amazing frameworks, and while they don't "give" you MVC, they definitely pave a roadway for you as a developer to create a beautifully built, MVC applications, considering that you, as the developer follow good MVC practices.

To those in this thread saying CodeIgniter is horrible. You are quite wrong, at least by today's standards. At the time of this post I wasn't into frameworks so I hadn't looked at it then. Therefor at that time it might have been horrible. But I use it ALL of my web applications now. They do a great job of creating a solid framework with an understandable directory structure, as well as give you some amazing tools to start MVC'ing away. They do a great job at logic distinction, they have an awesome community for support, and they overall provide a great, free framework which, again if you follow good MVC practices, can turn into some amazing applications.

To the main point. I wanted to interject my opinion on this subject. You'll never find that perfect, MVC built, out-of-the-box framework. However, if you utilize good MVC practices, and choose a solid framework such as Zend, Laravel and/or CodeIgniter you'll be fine. Because the raw truth is, that the MVC is up to the developer how s/he implements good practice and not the framework. The framework offers the foundation, the rest is up to the developer.

Good Frameworks to use as references

  • CodeIgniter
  • Zend Framework
  • Laravel
  • Symfony 2 (Added after reading comment, because it is also an excellent framework)

Just remember when using these, it is up to you as the developer to follow the good MVC practices. There are tons and tons of tutorials out there on good MVC practices. I stumbled upon a gentleman's website that has some amazing guidelines, and he aims towards proper MVC, and so far his blog posts have been pretty dang good, and he is a great place to start learning.

Reference

share|improve this answer
    
I understand this post is year old, which is ironically the reason I am posting this response. The question is actually now considered off-topic and I have voted to close it as such. One of the reasons that we now consider such posts as off-topic is precisely because the information is outdated so easily. –  Martijn Pieters Mar 15 at 18:34
    
I was wondering why you left out Symfony 2. Then I saw the reference to Tommy and it all became clear. –  Cerad Mar 15 at 20:15
    
You're right. I did forget symfony. I always forget that one because I seldom use it. But I am curious, how did my reference to Tommy "make it Clear"? What does that mean =P –  Jason Mar 15 at 20:28
    
Well, lets just say that a few years ago Tom annoyed one of the more serious developers enough that the developer got out his can of bug spray. Tom is fun to read but lives in a different reality. –  Cerad Mar 15 at 20:48
    
I won't disagree there lol. He is picky and very interesting and most definitely lives in his own reality. But I do admire his Model View Confusion series. And to me, he has a great approach on proper MVC. But that's just my opinion. Thank for the clarification @Cerad –  Jason Mar 15 at 20:51

It seems like you are just having trouble "putting it all together" because many tutorials are poor at explaining MVC in a clear way. I recommend that you get your hands "dirty" and build a simple application with about 3 to 5 different frameworks to see how they typically approach the problem. Some have documentation on how to setup the basic MVC architecture for an application and if they don't you can always google for a couple mvc tutorials for that particular framework.

FWIW I liked Zend's project/directory layout recommendation: http://framework.zend.com/manual/1.12/en/project-structure.project.html

Also take a look at Rasmus' "The no-framework PHP MVC framework". It shows you don't really need a framework, you can just build your own to suit your specific needs. Plus you'll get a kick out of the comments! http://toys.lerdorf.com/archives/38-The-no-framework-PHP-MVC-framework.html

share|improve this answer
    
Yes, I admit I can't gather all together into one piece. Your link to Rasmus' article cleared some doubts though.Especially the main idea, 'no need of framework to implement MVC pattern'. Thanks! –  kidonchu Apr 18 '13 at 18:17

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