Archive for the ‘Design Pattern’ Category

An Introduction to MVC

April 23, 2008

What is MVC?
A car is a good real-world example of MVC. With a car you have two views: the interior and the exterior. Both take input from the controller: the driver. The brakes, steering wheel and other controls represent the model: they take input from the controller (driver) and hand them off to the views (interior/exterior) for presentation.

MVC on the Web
MVC is an Object Oriented (OO) design pattern. A Model-View-Controller uses class to organize Business Logic contained in “Models”, Presentation Logic in “Views” and has an overall flow for the application within a “Controller”.

Figure 1:

The Model
Models are the portion of the MVC application that implements the “Business Logic”. Business logic is any PHP logic relating to how information is stored in your application.

The View
Views are the portion of the MVC application that presents output to the user. The most common output for PHP web applications would be HTML, but views are not restricted to this. Your MVC application might output XML, WML, plain text, images, email or some other content.

The Controller
The controller is the heart of the MVC application. The controller translate the mouse and keyboard inputs from the user, informing the model and/or the view to change as appropriate.

Figure 2:


Benefits and drawback of MVC details here.

TrygveReenskaug quoted about MVC as follows:
• The View is connected to the user’s eyes;
• the Controller is connected to the user’s hands;
• and the Model is connected to the user’s mind.

References:
http://www.phpwact.org/pattern/model_view_controller
http://www.onlamp.com/pub/a/php/2005/09/15/mvc_intro.html?CMP=AFC-ak_article&ATT=Understanding+MVC+in+PHP
http://en.wikipedia.org/wiki/Model-view-controller

Advertisements