MVC in PHP


Introduction

MVC stands for Model-View-Controller. Model means data, View means representation and Controller means business logic. MVC was originally described in terms of a design pattern for use with Smalltalk by Trygve Reenskaug in 1979.  MVC is a software architecture or we can say that MVC is a design pattern or MVC is a framework. The MVC pattern is a process of breaking an application into three parts: the model, the view, and the controller. A Design pattern is a code structure that allows common coding frameworks to be replicated quickly.

1st.gif

MVC is used in software development; the fundamental principle of MVC is based on the idea that the logic of an application and presentation are separated from each other i.e. MVC is simply a better way of separating the logic of your application from the display. MVC is an design pattern which is used in software programming. In MVC, the model represents data of the application; the view represents presentation logic and the controller represents all our business logic and manages the communication of data and the business rules. The MVC architecture has been used in many programming languages and technologies such as Java, .NET and PHP etc.

The purpose of the MVC pattern is to separate the model from the view so that changes to the view can be implemented, or even additional views created, without having to refactor the model.

A simple way to think of this would be to consider the following:

  • Model handles all our database logic.
  • Using the model, we connect to our database and provide an abstraction layer.
  • A user interacts with the view - by clicking on a link or submitting a form.
  • The View checks the state of the Model and responds accordingly.
  • View represents our presentation logic i.e our HTML/XML/ code.
  • The Controller handles the user input, and transfers the information to the model.
  • The Model receives the information and updates it's state.
  • The Controller represents all our business logic i.e. all our ifs and else.

 2nd.gif

Example

A bike is a real-world example of MVC. With a bike you have two views: the interior and the exterior. Both take input from the controller: the driver. The brakes, 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.

Model

Model is the first part of the MVC pattern. Model handles all our database logic. The Model may not necessarily have a persistent data store (database), but if it does it may access it through a separate Data Access Object (DAO). The main features of a Model are as follows:

  • A model represents data e.g. a database table.
  • The model manages the behavior and data of the application domain.
  • The model represents data and the business rules that govern access to and updates of this data.
  • The model is the piece of data that represents the state and low-level behavior of the component.
  • Model manages the state. The model has no specific knowledge of either its controllers or its views.
  • A model can have more than one view.

View

  • The view is the piece that manages the display of the state represented by the model.
  • View represents our presentation logic i.e. our HTML/XML/ code.
  • A view is some form of visualization of the state of the model.
  • The view manages the graphical and textual output.
  • It accesses enterprise data through the model and specifies how that data should be presented.
  • A view attaches to a model and renders its contents to the display surface.

Controller

  • The Controller represents all our business logic.
  • Using a controller we can change the state of the model. 
  • A Controller accepts input from the user and instructs the model and view to perform actions based on that input.
  • The controller translates interactions with the view into actions to be performed by the model.
  • The Controller provides the mechanism by which changes are made to the state of the model.
  • The Controller handles the HTTP GET and POST requests.

There are many frameworks available which follow the MVC pattern in PHP. Like as Zend Studio framework, CodeIgniter framework, PHP Smart etc. In the next article I will explain the CodeIgniter framework which follows the MVC pattern.

Conclusion

So in this article you saw an introduction to MVC in PHP. In the next article you will see the MVC pattern used in the CodeIgniter framework. 

Some Helpful Resources

Up Next
    Ebook Download
    View all
    Learn
    View all