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
add comment

4 Answers 4

up vote 9 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
4  
... 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
5  
-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
show 3 more comments

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
add comment

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
add comment

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