Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
Retiring, March 2020 - sorry, you have missed our final public course.
The Coronavirus situation has lead us to suspend public training - which was on the cards anyway, with no plans to resume

Please ask about private 'maintenance' training for Python, Tcl, Perl, PHP, Lua, etc
Happily continuing private consultancy / programming work
The Model, View, Controller architecture (MVC) - what, why and how.

You may have heard the "Model, View, Controller" mantra being bandied about, but what does it mean, why are some people so keen on it, and does it work? Where is it used?

What does MVC (Model, View, Controller) really mean?

It means that you divide your program / application into three areas.

The "model" is the section which defines the data that you're holding - it defines the database tables, what the columns in your tables are, what's valid and what's not valid, and how the tables may be linked ("joined") to each other.

The "view" is how the data is presented to the user - what the screens look like and which information goes where. For all but the more simple models there will be multiple views.

The "controller" is the code that sits between the model and the view; methods in the controller collect the data from the model and perform any actions necessary on it to display it to the user. And the take new or amended data entered onto a view (usually via a form), and save it validated into the model.

Model, View, Controller - how they fit together

Why are people keen on MVC?

The detachment of the model from the controller means that the model and the business logic that's closely associated with it can be separately tested and maintained, and can be used by various different controllers. So the same model can be used for a controller that looks after web interaction and data entry, and another controller that look after monthly statistics from the database in a batch mode.

Similarly, the views are logically separated from the controller, allow one team of people to look after the control logic and another team to look after the look and feel of the application - how it prsents to the user. To a very great extent each can be tested independently, and the look and feel (the view) can be changed without effecting the underlying business logic.

By providing this separation of tasks, each section can be provided by experts at that section - good programmers aren't often good graphic artists (and vice versa), and database design is a strange discipline which has its own specialised practitioners.

Maintainance of the system as a whole is made much easier as issues that need work are quickly tied down to one area, and the structure encourages template and code reuse. So - a good and efficient scheme of things.

Does it work?

Yes, it does ... and all but the very small web application. To divide an application that does no more that echo the current date onto a page into three - an empty model, a controller that reads the clock, and a viewer that bursts the hours and minutes through onto a web page would be overkill, but very quickly it become worthwhile.

Our online course booking system was built into an MVC based framework in PHP, based on Rasmus Lerdorf's 4 layer model (in effect M, V, C with the encompassing framework being the fourth layer), and I wrote it in a couple of days - without the framework to start from, it would have been a week's work. And it's easy to maintain too - I added a new course - Ruby on Rails to my model today, and it went in quickly and easily without any major reworking. The controller picks up the new data from the model, and it's viewed - literally a five minute job and you can book for 10th April ;-)

Where is MVC used?

I've given you the clues that we use it, in PHP, for much of our web site work. We were early practitioners, before even the formallity of MVC design came to the front, so it's a bit fuzzy in places - but it's there. And it's also very much there in templating toolkits like Smarty in PHP, and it's used in iOS - the Apple operating system that's used for iPhone and iPad programming.

In Python, the Django Framework implements the MVC model, and Ruby on Rails is an MVC based framework through and through.

You'll find MVC used as the structure within major open source applications too. The Moveable Type software that we use for the blog, the Simple Machines Forum that's inside our "Ask the Tutor" forum and many (most?) others use MVC in name, or if not in name, but in deed.

This is one of a series of summaries taking you from initial installation of Ruby and Rails through to a complete multitable application:
[link] - Installing Ruby and Rails
[link] - Hello World, Ruby on Rails Style
[link] - What and Why - Model, View, Controller
[link] - Multiple views, same model
[link] - A form to allow data to be added to the model
[link] - Validating data entries for storage in the model
[link] - Cleanly viewing model data
[link] - Complete sample, including a multiple table model
These topics are covered on our Introduction to Rails day - an optional extension of Ruby Programming or Learning to program in Ruby

(written 2010-02-01, updated 2012-06-23)

Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Q915 - Object Orientation and General technical topics - Principles of Model - View - Controller
  [4691] Real life PHP application using our course training MVC example - (2016-06-05)
  [4641] Using an MVC structure - even without a formal framework - (2016-02-07)
  [4527] Hello Flask world / Python web micro framework - (2015-10-11)
  [4391] Refactoring Perl applications to give them a rosy future - (2015-01-11)
  [4320] An example of Model-View-Controller techniques in a Perl / CGI script - (2014-11-20)
  [4114] Teaching CodeIgniter - MVC and PHP - (2013-06-12)
  [4066] MVC and Frameworks - a lesson from first principles in PHP - (2013-04-19)
  [4010] Really Simple Rails - (2013-02-17)
  [3919] What is a web framework? - (2012-11-10)
  [3705] Django Training Courses - UK - (2012-04-23)
  [3624] Why do we need a Model, View, Controller architecture? - (2012-02-25)
  [3454] Your PHP website - how to factor and refactor to reduce growing pains - (2011-09-24)
  [3237] Using functions to keep look and feel apart from calculations - simple C example - (2011-04-09)
  [2199] Improving the structure of your early PHP programs - (2009-05-25)
  [687] Presentation, Business and Persistence layers in Perl and PHP - (2006-04-17)

Back to
Sunday Evening, City of London
Previous and next
Horse's mouth home
Forward to
Constants in Ruby
Some other Articles
Defining a static method - Java, Python and Ruby
String to number conversion with error trapping in Ruby
Neatly formatting results into a table
Constants in Ruby
The Model, View, Controller architecture (MVC) - what, why and how.
Sunday Evening, City of London
Cheat Sheet - what do you need for Ruby on Rails?
Scope of variables - important to Ruby on Rails
Search and replace in Ruby - Ruby Regular Expressions
Answers on Ruby on Rails
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page

This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2020: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2612_The ... -how-.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat