The model-view-controller pattern

The MVC pattern: What it is and how it can help you write better code

The Model-View-Controller (MVC) pattern is a way to organise your code that separates it into three parts: models, controllers, and views It allows you to separate the code that does stuff from the code that shows stuff.

The model-view-controller pattern


  • Models are where an application’s data are stored, usually in a database.
  • Responsible for storing and retrieving data.
  • They know nothing about the user interface.


  • Views are what the user sees on the screen (i.e. the HTML).
  • They present the data to the user.
  • Know nothing about the models.


  • Controllers are what the user interacts with.
  • They receive a request from the user, decide what to do, and send a response back.
  • It’s the only component that interacts with the models.

Why use MVC?

Organising your code according to the MVC pattern allows you to separate concerns: keep application code separate from presentation code.

  • It’s easier to reuse code, so developing is faster.
  • Code is more organised, so it’s easier to understand and maintain.
  • It’s easier to test the code.
  • it’s more secure.

Developer specialisation

Another big advantage is for the developers working on your project. Designers can focus on the front end without worrying about the business logic. The developers of the models can focus on the business logic or back end without worrying about the look and feel. For example, if you want to change the look and feel, you don’t have to change anything to do with the database; and vice versa.

Learn more by writing your own PHP MVC framework from scratch, my latest online Udemy course.

Dave Hollingworth
IT Trainer and Web Application Developer