What is MVC?

Understanding the Model-View-Controller pattern and why separation of concerns matters

The MVC Pattern

MVC (Model-View-Controller) is the most common pattern for organizing web applications. It separates data, presentation, and logic — making applications easier to understand, test, and modify.

MVC divides an application into three interconnected components, each with a distinct responsibility:

  1. Model — manages data and business logic. It knows how to read from and write to the database, validate data, and enforce business rules. The Model has no knowledge of how data is displayed.
  2. View — handles presentation. It takes data from the Model and renders it into HTML, JSON, or whatever format the client needs. The View has no knowledge of how data is stored.
  3. Controller — handles user input and coordinates the flow. It receives requests, asks the Model for data or tells it to update, then passes the result to the View. The Controller is the traffic cop.
                              +----------------+
                              |  Controller    |
              Request         |                |
  USER ---------------------->|  Handles       |
                              |  input         |
                              +-------+--------+
                                      |
                           +----------+----------+
                           |                     |
                           v                     |
                    +-------------+              |
                    |    Model    |              |
                    |             |              |
                    |  Data &     | <------>  Database
                    |  Logic      |
                    +------+------+
                           |
                           v
                    +-------------+
                    |    View     |
                    |             |         Response
                    |  Renders    | ---------------------->  USER
                    |  output     |
                    +-------------+

Why Separate Concerns?

To understand why MVC matters, consider what happens without it. Here is a single PHP file that handles routing, database queries, and HTML rendering all in one place:

Without MVC: Everything in One File

query('SELECT * FROM users'); $users = $stmt->fetchAll(PDO::FETCH_ASSOC); ?> Users

User List

prepare('INSERT INTO users (name, email) VALUES (?, ?)'); $stmt->execute([$name, $email]); header('Location: /users.php'); } ?>

This works for a small script, but it becomes unmanageable as the application grows. Every file mixes SQL, HTML, and routing logic together. Now consider the same logic split into Model, View, and Controller:

With MVC: Separated Responsibilities

// -- models/User.php -- class User { private $pdo; public function __construct($pdo) { $this->pdo = $pdo; } public function getAll() { $stmt = $this->pdo->query('SELECT * FROM users'); return $stmt->fetchAll(PDO::FETCH_ASSOC); } public function create($name, $email) { $stmt = $this->pdo->prepare('INSERT INTO users (name, email) VALUES (?, ?)'); return $stmt->execute([$name, $email]); } } // -- views/users/index.php -- Users

User List

// -- controllers/UserController.php -- class UserController { private $userModel; public function __construct($userModel) { $this->userModel = $userModel; } public function index() { $users = $this->userModel->getAll(); require 'views/users/index.php'; // $users is available in the view } public function store() { $this->userModel->create($_POST['name'], $_POST['email']); header('Location: /users'); } }

The same functionality, but now each piece has one job. The benefits compound as the application grows:

Benefits of MVC

Benefit How MVC Helps
Testability You can test the Model (data logic) without rendering HTML, and test the View without a database. Each piece is testable in isolation.
Team Workflow A frontend developer can work on Views while a backend developer works on Models. They don't step on each other's code.
Reusability The same Model can serve an HTML view, a JSON API, and a CSV export. The same View template can display data from different Controllers.
Maintainability Need to change how users are displayed? Edit the View. Need to change the database schema? Edit the Model. Neither change affects the other.