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