Introduction to MVC software pattern

June 01, 2022

The best option is to separate the software pieces that make up the site code into models, views (templates) and controllers.


Model stores and returns the data necessary to generate resulting web pages

The models are written in PHP and are almost always implemented as classes. Typically, the data manipulated by the model is stored in a database, but in the simplest cases, it can be written in a text file or even a regular PHP array.


Template describes the web page generated by site

The template is usually written in HTML with some fragments of PHP code (and therefore it is a server-side page).


  • Runs when we receive a client request (from browser);
  • Retrieves the required data from the model;
  • Potentially saves some data received from the user back to the model;
  • Generates a response - a resulting page based on the template used.

The controller is written in PHP and is usually a separate class (although in the simplest cases it can be written as a function). The controller is invoked directly by the router when it receives a request with a path that matches a certain pattern. Models and templates are invoked by the controller as soon as they are needed. We can say that the controller plays the main role here, the model and template - secondary. Models, templates and controllers are created in such a way that they do not depend on each other and should stay universal as much as possible. Any controller can use arbitrary models and templates, and any model and any template can be used by an arbitrary controller.


This architectural pattern assumes division of the program code into models, templates (views) and controllers. This has a number of benefits:

  • Easier to update code because different tasks are in different files;
  • If we want to change the way we store data, we only have to change the model - no need to touch the controller or templates;
  • If we want to change the way our website behaves - it’s enough to update the controller, no need to touch models or templates;
  • If we want to change the way a web page looks - it is enough to adjust the template, no need to modify models or controllers;
  • It’s easier to create and test different parts of our application;
  • Ability to reuse code - since each module (eg. model) is self independent. As a result, for example, the same model can be used by different controllers;
  • Different parts can be written by different developers. Models and controllers are often written by PHP developers, while templates are written by front-end developers.

Popular MVC frameworks

Many of the modern software frameworks actively use MVC pattern:

Published by Artiphp who lives and works in San Francisco building useful things.