I want to learn MVC "architecture pattern" but I don't want to jump into a framework like Rails or Django just yet. I want to understand the concept first and write some simple code in my currently familiar environment, which happens to be php/html/css/mysql. I don't necessarily need a tutorial that is based on php, as I do understand a lot of different languages. And I don't want to have to install any frameworks or APIs or libraries. I just want to learn how to think in MVC and apply it to my projects. Any suggestions?
Almost every framework does MVC differently, so you might end up getting even more confused. The general principles of MVC are very simple: "Model is state; view reacts to model; controller reacts to view; controller changes model". The model, view and controller are concepts - they are whatever you feel them to be. Classes, bunches of classes, instances of classes with XML configuration files, you name it.
I actually think that about covers the basic principles. Without a framework, you'd not get much further. What matters is how a particular framework defines model, view and controller and their interactions.
One of the most interesting resources are the original papers of Trygve Reenskaug. Wikipedia also has a lot of language-agnostic information on MVC.
I didn't understood the MCV pattern until I tried it. If you are familiar with PHP you can try http://cakephp.org, its a PHP framework wich uses the most of the RoR paradigms.
In addition to Sander's reply, I'd say that most frameworks confuse front controller and MVC. They are really two completely separate concepts, but they are often both present in frameworks. So watch out for that.
Check out this description, example, and diagram that cover the basics of MVC.
MVC is great setup for simple designs but it's often confused with PAC which is similar and further development on MVC. Both setups are great the the two links give information to help them be understandable.
Try this great article: The no-framework PHP MVC framework. It's not a substitute for an introduction to the MVC pattern, but it provides simple and hands-on examples.
The main advantage of MVC is separation of concerns. When you write code, and if you're not careful, it can become a big mess. So knowing how to put Models, Views, and Controllers in different "silos" saves you time in the long term. Any strategy is good.
So here is mine :
- models are files found under /lib in the project tree
- views are files ending in .html in the project tree
- controllers are urls in <form> action attributes
Don't ask me how, but I came across this entry from an excellent wiki on the subject of design patterns:
http://www.c2.com/cgi-bin/wiki?ModelViewController
Lots of interesting discussion and pointers to various resources.
And it only took me six days to find it!
MVC is basically just splitting up your code into a Model, which deals with the data, a View which displays the data, and a Controller which passes data from the Model to the View.
It's nothing you need an API or framework for, it's just a way of splitting up your code. The reason many frameworks use it is because it's a very simple concept, it works well for many things (it fits webpages perfectly), and is fairly flexible (for example, with Rails, you could do everything in your view, or model/controller, if you so-desired..)
A quick example in python, of an example MVC structured Python script. Not necessarily "best practices", but it works, and is fairly simple:
class Model:
def get_post(self, id):
# Would query database, perhaps
return {"title": "A test", "body": "An example.."}
class Controller:
def __init__(self):
self.model = Model()
self.view = View()
def main(self):
post = self.model.get_post(1)
self.view.display(post)
class View:
def display(self, item):
print "<h1>%(title)s</h1>\n%(body)s" % item
c = Controller()
c.main()
You can try this PHP MVC Tutorial. It's well written, very light, contains only the essentials and you can find the code on sourceforge.
This tutorial will take you from the beginning to the end of building a MVC framework. The object is not soley to produce the finished MVC framework, although that will happen, but to demonstrate how MVC works and some of the concepts that lay behind it..