Posted by & filed under CodeIgniter, ORM, PHP.

The Model View Controller is a software architecture which separates the representation of the information from the user’s interaction with it.  The architecture, as the name suggests, contains 3 main components. viz. The Model, The View and The Controller.


The Model

The model is the component that holds all the business logic. The model is responsible for manipulating the data, handling the database, structuring the data for the view, etc. In classic MVC, the model notifies the view about any change in data.

The View

The view is the presentation layer for the system. The view takes care of all the presentation related stuff.

The Controller

The controller basically sends commands to the model to update the models state. It also sends commands and data to the view to represent the data received by the Model.

How to use an ORM in an MVC styled application?

If you are using an ORM (as discussed in a previous post) in your MVC styled application, The ORM will be responsible for generating your Models completely. They are actually called entities in the ORM, but they are the Model classes.

MVC with PHP

It is impossible to write classical MVC in PHP. Any framework which claims you can, is lying to you. To implement a classical MVC it would require your Model to inform the View about the changes, which too is impossible in your plain PHP page (you can do something close to classical MVC, if you use sockets, but that’s impractical for real website).

There are 4 other MVC-inspired solutions for Web development.

1. Model2 MVC
Model 2 MVC is a design pattern for web applications in JAVA. It basically separates the display of content from the Logic.

2. MVVM – Model View ViewModel
MVVM is aimed at UI development platforms which support the event driven programming. Here, the Controller is swapped out for a ViewModel, which is responsible for the translation between View’s expectations and Models’s logic.

3. MVP – Model View Presenter
MVP is probably the closest derivative of classical MVC where the Presenter requests information from Model, collects it, modifies it, and passes it to the passive View. This is what most PHP frameworks pass as MVC.

4. HMVC – Hierarchical MVC
HMVC is a very distinct variation of Model2 where the controllers have the right to execute sub-controllers. This gives modularity but hits performance badly.

Frameworks like CodeIgniter and Zend use the MVP architecture. i.e. The client (web browser) requests are sent to the Presenter. The Presenter requests information from the Model, manipulates it a little bit and passes it to a view for Display.
Most of the code I write in based on CodeIgniter, so most of the posts on this blog will follow the MVP pattern.

Leave a Reply

  • (will not be published)