Understanding MVC design pattern in PHP

Understanding MVC design pattern in PHP
Understanding MVC design pattern in PHP

Disclaimer: This is beginners tutorial

MVC stands for Model, View & Controller. It is a software design pattern that divides the application into the interconnection of three main components. Let’s see understanding MVC design pattern in php.

Each of these components is also called a layer like a data layer, presentation layer, and business logic layer.

Evolution of MVC:

So MVC pattern was invented by Trygve Reenskaug while he was a visiting scientist at Xerox Palo Alto Research Laboratory (PARC) in 1978/79. In 1978, he wrote his first paper on MVC. We can read his original papers here:

Okay, now we know the history. Now let’s jump right into what each component is.

Understanding MVC design pattern in PHP
Model-View-Controller Diagram

The Model:

A model is a place where all the business logic of an application is stored. It can be anything from storing application data to accessing third-party services to fulfill its business requirements. Mainly all the heavy operations are performed in the model regarding database operation.

Model/data layer acts as a memory for the requested service. Request from a user which requires data manipulation must be sent to model to perform DB operation like storing data, accessing data, update/delete data.

In the above diagram, model is drawn a little bit big because of its responsibilities. All the requests from view come via controller to serve a requested operation. So, once that operation is performed then required data is sent back to view.

The Controller:

The controller acts as an interface between view and model. Its responsibility(mostly) is to get the request from view and send it to model to perform a necessary operation. This means it separates user interface from business logic and handles how the application will respond to user interaction in the view.

Controller acts as an entry point for every request because the request is first passed to a controller & then models will be instantiated and once we get the data then model returns data to the controller and its task is to send to respective view.

Sometimes, if required, then the controller also requests some third party services OR helpers OR some other service layer to restructure data returned from the model so that it will be easy for a view to arrange that data properly on the view.

In the diagram above, we can see that, every request from the user will first be received by controller in the trio and if required then it will request models to operate on data otherwise it will be handed over to view to show respective UI to the user.

The View:

View is the place where user will see its expected result. View is responsible for sending requests to the controller and receives a response from it. It is a place where all the templates are stored which contains HTML, CSS, and JS files.

A view can be a single HTML file OR an amalgamation of different templates divided in chunks and combined as per the requirement of the request OR to make it more dynamic because there are some common areas in the template which will be required at different parts of the whole project. So keeping it in a separate template will make it more manageable.

As per the diagram, it has been placed at the end because view will be decorated once all the necessary data from the controller is received. This data will be placed at different locations of the view and finally served to the user.

MVC Based Frameworks in PHP:

There are so many PHP frameworks available for use. But only few of them actually follow MVC kinda architecture principles. Here is the list of such frameworks-

CodeIgniter ( CI ):

CodeIgniter is an open-source PHP web development framework for building dynamic web sites in PHP.

CI is developed and maintained by EllisLab and is most often noted for its speed when compared to other PHP frameworks. Its initial release was published on February 28, 2006. The current stable version of CI is 4.0.4 (till date).

I have worked in CI in many projects. Basically this framework is very easy to understand. We can also customize it based on the requirement. It explicitly contains Model, View, and Controller folder which represents that it follows MVC architecture.

Laravel:

Laravel is also an open-source PHP web development framework created by Taylor Otwell and follows the model–view–controller (MVC) architectural pattern and based on Symfony.

The initial release was published in June 2011. It was developed as a more advanced alternative to the CodeIgniter framework, which does not provide certain essential features such as user authentication and authorization.

Symfony:

Symfony is also based on the MVC architecture, which also consists of three levels like model, view, and controller.

Symfony was inspired by the spring framework. The initial release was published on 22nd October 2005.

Advantages of MVC:

MVC is basically a separation of concern.

Before the rise of MVC based frameworks in PHP, most of the projects were managed using core PHP methodology where view, logic, and database operation performed on the same .php file. Problem arrives only when the same piece of functionality requires at different locations of the project.

So, once MVC introduced in the web development and different PHP framework adopted it in clean way then life of developers became very easy.

Let’s different advantages of using MVC software design pattern in the development.

  1. Faster application Development.
  2. Multiple developers to work together simultaneously.
  3. Easy to Update & debug an application.
  4. Ability to divide views in chunks

These are some of the advantages of using MVC over classical web application methodology.

Conclusion:

A good MVC based application always means understand the principles of the architecture in a better way to apply all the necessary rules to the application at each location whenever necessary. It is always up to the developers that work on the framework to keep things in the right order. A good MVC based application needs to cover all the important aspects of the MVC framework to succeed. Now we have seen understanding MVC design pattern in PHP, we’ll soon come up with an example and an in-depth understanding of MVC framework flow.