MVC Architecture (Introduction)
Understand the Model-View-Controller pattern conceptually and see how it organizes growing PHP applications into maintainable pieces.
Introduction
Up to this point, most of the scripts you have written have mixed everything together in one place: database queries, business logic, and HTML output, all in a single .php file. That works fine for small pages, but as an application grows to dozens or hundreds of files, that mixing becomes a serious problem — a change to how data is displayed can accidentally break the logic that fetches it, and vice versa.
MVC (Model-View-Controller) is a design pattern that solves this by splitting an application into three clearly separated responsibilities. It is not a PHP-specific idea — it is used across many languages and frameworks — but it is especially central to how modern PHP applications, including Laravel and Symfony, are structured.
- The problem that MVC is designed to solve.
- What Model, View, and Controller each mean.
- How a single request flows through Controller, Model, and View.
- A simple folder structure that reflects MVC.
- How this concept forward-references frameworks like Laravel and Symfony.
The Problem MVC Solves
Imagine a single PHP file that connects to the database, runs a query, applies business rules, and then prints HTML — all in one script. It works, but it has real costs as the project grows.
Tangled Responsibilities
Data access, logic, and HTML are all mixed together, so a small change anywhere risks breaking everything else.
Hard to Reuse
You cannot easily reuse the same data-fetching logic for an HTML page, a JSON API, and an admin panel.
Hard to Test
Logic wrapped inside HTML output is difficult to test in isolation, since you cannot run it without also rendering a page.
Hard to Collaborate
A designer working on HTML and a developer working on logic keep colliding in the exact same files.
MVC addresses this by giving each concern its own home, so each piece can change independently of the others.
What is MVC?
MVC divides an application into three parts, each with one clear job.
Model
Represents data and business logic — for example, fetching a user from the database, validating a price, or calculating a total. The Model does not know anything about HTML.
View
Represents what the user sees — the HTML template that displays data. The View does not fetch data or contain business rules; it just renders whatever it is given.
Controller
Handles an incoming request, decides what to do, asks the Model for data, and passes that data to the right View to render. The Controller is the coordinator, not the worker.
- Model = the data and the rules around it.
- View = the HTML template the user actually sees.
- Controller = the traffic cop that connects a request to the right Model and View.
How a Request Flows Through MVC
When a visitor requests a page, the request does not go straight to a View. It is routed to a Controller first, which orchestrates everything else.
Because the Controller sits in the middle, the Model never needs to know about HTML, and the View never needs to know about databases. Each piece can be changed, tested, or replaced without touching the others.
A Tiny Folder Structure Example
MVC is usually reflected directly in how project files are organized. A minimal example might look like this.
app/
├── Controllers/
│ └── ProductController.php
├── Models/
│ └── Product.php
└── Views/
└── product-details.phpEach folder maps directly to one of the three responsibilities: Controllers/ handles requests, Models/ handles data and logic, and Views/ handles what gets displayed.
A Simplified MVC Example
This is a deliberately simplified illustration — real frameworks add routing, dependency injection, and more structure — but the core idea is exactly this separation.
<?php
class Product
{
public static function find(int $id): ?array
{
$products = [
5 => ['id' => 5, 'name' => 'Wireless Mouse', 'price' => 19.99],
];
return $products[$id] ?? null;
}
}<?php
require __DIR__ . '/../Models/Product.php';
class ProductController
{
public function show(int $id): void
{
$product = Product::find($id);
if ($product === null) {
echo "Product not found.";
return;
}
require __DIR__ . '/../Views/product-details.php';
}
}<h1><?= htmlspecialchars($product['name']) ?></h1>
<p>Price: $<?= number_format($product['price'], 2) ?></p><?php
require __DIR__ . '/Controllers/ProductController.php';
$controller = new ProductController();
$controller->show(5);Wireless Mouse
Price: $19.99Notice that Product knows nothing about HTML, product-details.php knows nothing about arrays coming from a database, and ProductController is the only piece that talks to both.
MVC in Real Frameworks
The example above is intentionally tiny — you would not hand-write require statements like this in a real production application. Popular PHP frameworks build entire, mature systems around the MVC pattern, adding routing, request objects, templating engines, and much more.
Laravel
One of the most popular PHP frameworks, structured almost entirely around Models, Views (using the Blade templating engine), and Controllers.
Symfony
A powerful, flexible framework that also follows MVC principles, and whose components power parts of Laravel itself.
You do not need to learn a framework to understand MVC — the concept stands on its own. But recognizing this pattern now means that when you do pick up Laravel or Symfony later, the folder structure and flow will already feel familiar.
Common Mistakes
- Putting database queries directly inside a View — Views should only receive data, never fetch it themselves.
- Putting HTML output inside a Model — Models should stay completely unaware of how data will be displayed.
- Making Controllers too "fat" by stuffing business logic into them instead of delegating to the Model.
- Assuming MVC requires a framework — you can apply the same separation in plain PHP, as shown above.
Best Practices
- Keep Models focused on data and business rules only, with zero HTML.
- Keep Views focused purely on presentation, with minimal logic.
- Keep Controllers thin — they should coordinate, not do the heavy lifting themselves.
- Mirror your MVC structure in your folder layout so it stays easy to navigate as the project grows.
Frequently Asked Questions
Do I have to use a framework to use MVC?
No. MVC is a design pattern, not a specific piece of software. You can organize plain PHP code into Models, Views, and Controllers yourself, as shown in this lesson.
Is MVC the only architecture pattern used in PHP?
No, there are others, but MVC is by far the most common pattern in PHP web frameworks, which is why it is worth understanding early.
What exactly goes in the Controller if not business logic?
The Controller reads the request, calls the appropriate Model method(s), and chooses which View to render with that data — coordination, not computation.
Can a View contain any logic at all?
A small amount of display logic (like formatting a number or looping over a list) is normal. Anything involving a database or business rules should live in the Model instead.
Will I learn Laravel or Symfony in this course?
This course focuses on core PHP, so this lesson only introduces the MVC concept those frameworks are built on. Learning a framework in depth is a great next step after finishing this course.
Key Takeaways
- MVC separates an application into Model (data/logic), View (presentation), and Controller (coordination).
- A request flows: Controller receives it, asks the Model for data, then hands that data to a View to render.
- Separating concerns this way makes applications easier to maintain, test, and grow.
- Popular frameworks like Laravel and Symfony are built around the MVC pattern.
- You can apply MVC principles in plain PHP without needing a framework.
Summary
MVC is one of the most important organizing ideas in web development. By keeping data (Model), presentation (View), and request handling (Controller) separate, applications stay easier to understand, test, and extend as they grow far beyond a handful of files.
In this lesson, you learned what each part of MVC means, how a request flows through all three, and how this same pattern underlies frameworks like Laravel and Symfony. Next, you will look at security best practices every PHP application should follow.
- You understand what Model, View, and Controller each represent.
- You can trace how a request flows through Controller, Model, and View.
- You have seen a minimal MVC folder structure and worked example.
- You know that Laravel and Symfony are built around this same pattern.