Apr 152013

This is one of the best concise explanations of the Model-View-Controller pattern I’ve run across. I can’t remember where I found it, so sadly I can’t give credit where credit is due.



Model: The model contains the server business logic code. This usually involves the reading and writing to a database in addition to some pre- or post-processing. For example: ‘A user enters a comment, but before we insert it into the database, we need to do a spam check with akismet.com; if it passes we can do the insert.’

View: This is where you present the output to the user in a particular format, most often HTML markup, although it can be the JSON or XML format. For example: ‘We need to display all the comments for a forum topic, the model fetches all the comments and are then read and formatted by the view.’

Controller: The controller is essentially the manager. It first intercepts the URL, then calls the correct models and views before presenting it to the browser, mobile device or an API caller. For example: ‘In displaying all the comments for a topic, the controller makes the actual call to the model and then passes the model data to the view, which then generates the HTML output. This generated view is then shown to the calling browser by the controller.’

 Posted by at 9:55 am