Have you ever constructed a larger site with raw HTML? Or PHP? How did you do the navigation? How did you make sure every page had the same layout? What happened if you had to change the layout? Did you go through all your 20+ pages?
MVC (Model View Controller) is about pure style, and with style I don’t mean your page layout. I mean your code design. MVC ensures that your code is compact, clean, lean. MVC is very easy to grasp. With MVC you can build sites with arbitrary number of pages.
The controller is the piece of your software that takes incoming requests (URLs) and dispatches them. It makes educated guesses based on your URL on where to dispatch the request. E.g., if a request to /admin comes in, your controller could decide to execute another piece of code, an “action”. This action could check whether there is a logged in user in the session. Or verify some request parameters.
Views get rendered by the controller, or the rendering of a view is triggered by the controller. In most cases a rendered view is an HTML page. Based on the incoming request, the controller decides which view to render. Different views usually stand for different layouts or areas of your site. Most of the time a view is a template. It gets its data injected by the controller (e.g., some navigation links and the page content) and then gets written as HTML to the client.
The model is the data of your application, the objects the controller will inject into your view so it can be rendered. In CMS, the page to display is a model. It’s an object with a title and content, your actual page content you want users to see. But your model can also contain a User object for representing users of your system. The model is the sum of all data objects you need for displaying your pages.
A CMS is connected to MVC. I can’t imagine any case of CMS without using MVC. This is because your content is already separate from your HTML pages to display, and you need a controller to display HTML pages out of the content (in your database or in a flat file system). If you write a CMS, no matter which technology you use, you will invariably end up using MVC. You will have at least one template. In this template you will inject your content and render it. You need one central instance which gets called on every request. This instance will load your content from a database and display it.
Tags: model view controller, mvc
February 19th, 2009 at 4:21 am
pretty good explanation, thanks for sharing!
July 25th, 2009 at 10:55 pm
Great but….
You have CMS which allows you to change theme. In other new theme you want to you change homepage where should be displayed last 5 pages that you created. So, you change homepage template and nothing else and You end up with view which decided what data should be displayed. It’s not MVC here.
Do You have solution for this?
July 25th, 2009 at 11:21 pm
In most template based CMSs the view decides a lot of things, since that’s the thing you can most easily change. And in your case I would even say that it’s still MVC, because the view decides what to display, and if that’s 5 pages it’s 5 pages. The controller still has to dispatch to your homepage, and will be the system to decide what to display on the next click.
And even if it weren’t clean MVC it’s still your safest bet to go with the template system your CMS is providing unless you want to rewrite a lot of code, or even start a CMS from scratch.
July 27th, 2009 at 6:35 am
@uberdose: Thanks. I think that I’ve got it.
March 8th, 2010 at 1:48 am
Your article is a nice explanation. I do however have some questions and if you’d be as kind to answer them, I’d be really greatfull.
I want to build a cms using the mvc architecture. It must support different types of sections, for example file collection type(images, files etc, ex for the ‘photos’ section), just a simple html (ex: ‘about us’ section), collection of htmls in a blog style (ex ‘announcements’ section or main page). I need it to be expandable. Let’s say someone wants to add a new type of section. He/she should be able to add a module of code, following some very basic given guidelines, and the module should then be able to interact with the cms.
So, I thought: Each section type, will have its sub-controller,sub-view,sub-model. There should be a main controller. It gets the request. Based on the arguments, it passes it down to the appropriate sub-controller(for example if the requested action is to rename a file of some file collection, it invokes the file collection sub-controller). The sub-controller communicates with the sub-model to perform the requested actions. I think, so far, so good.
Now, the view part is where things get a bit complicated. First of all. I’m not entirely sure as to what the view in the mvc pattern is supposed to do. Does it just get the data from the controller, and prints it in html? Or does it communicate directly with the model, to get the right data? Let’s take also another scenario. Let’s suppose that in one page, I want to display data from different section types, thus different modules, each one with its separate sub-view etc. How do all the sub-views get compiled together, if all the data is sent from the main controller? Would it be right to use the main controller only for the ‘actions’ request, and forward all the display requests to the main-view? I’m a bit confused as you can understand.
Take this blog of yours for example. In the main page, it shows all the recent post, with only the first few lines of each post’s body. If you browse to a certain post, it shows only that post, plus the comments in the bottom and the post comment form. So, the controller takes the request. What happens after that? Who communicates with the model to get the data? Who decides if the comments form will appear or not? The controller or the view? Could you possibly clarify any of this? Thanks in advance. Goodnight.
ps: feel free to contact me with email, if you prefer