So you want to learn about MVC? This article will hopefully clear some of your possible confusion when it comes to this three letter acronym that can strike fear into the hearts of many. Like most topics in web development, it’s always a little confusing when you first take a look at it, but after you read up on the topic, and try it for the first time, it’s as easy as pie! Read on, my friend!
MVC stands for Model-View-Controller. It is an architectural pattern used when engineering software. Basically, it separates the three main parts of an application and makes them independent of each other, therefore they can be designed, tested, and worked on independently of each other.
In the world of web development, MVC is usually used with a framework such as CakePHP or CodeIgniter. For the purposes of this post, we won’t be focusing on any individual framework, more on the idea of what MVC really is and how it can be used. If you would like to delve into one framework as a beginner, though, I highly recommend CodeIgniter to get started. It is simple, yet powerful, and won’t confuse you when you’re getting started.
Models are very simple. They are the knowledge of your program, they represent it as either a single object, or a structure of multiple objects.
In the static web world, your model could be simply the HTML of your page. It is the skeleton of your content, how it will be structured, and of course, the content itself.
The view is a visual representation of a model. In a program, a view fetches the model’s data, and displays it. It can also update the model by collecting data.
A static example of this is plain old CSS. The CSS adds visual style to your model (the HTML), it is used as the “skin” to the skeleton of your content. We can switch from one style to another and not affect the model at all.
The controller is what puts it all together between the user and the system. It receives user’s input in the form of commands and data, then outputs the views that are to be presented.
An example of the Controller would be the browser. It takes the HTML, CSS and any other jargon and outputs a readable web page for the user to interact with. It can also accept user input in a sign up form for example.
Hopefully I haven’t confused you too badly, and you have at least a basic understanding of MVC now. It’s literally all around you, as I have shown in a simple HTML/CSS/Browser example. If you want to read more, the CodeIgniter documentation explains it in terms of their own framework. Take a look at those docs here. Thanks for reading!
December 29th, 2009 at 2:01 pm
A model in MVC should never contain HTML code. Markup is also a part of the view. The model is where you put your logic, your database queries etc.
Well written for a 15 year old thought, keep it up ![]()
December 30th, 2009 at 12:47 pm
I think the “HTML-CSS-browser == MVC” formulation is sort of wrongheaded, although to some extent, I suppose, it comes down to what the meaning of ‘is’ is.
There was a fairly epic throwdown at Coding Horror a while back which explored both side of this issue pretty thoroughly, and is well worth the read.
http://www.codinghorror.com/blog/archives/001112.html
@Karl:
Yes, I know that a model should never contain HTML. I’m simply providing an example of what a model is, in essence.
I like MVC , It is more manageable framework ![]()
I will not say that i was aware of MVC because before reading your post it was just a name of a tool for me but now i got what it is. Thanks For sharing this for us.
Twitter
Follow me on Twitter to keep up to date!
RSS Feed
Keep up with all of our updates by subscribing to our RSS feed!
FaceBook
Join our group on Facebook and become a fan of us!