Three jobs, three names.
MVC summarizes three roles. They are not three separate programs: they are three ways to organize work inside the same application.
The book title, its author, and the rule determining whether it is available.
The title, cover, and button displayed in the browser.
It receives number 12, asks for that book, then selects the page to display.
Example: display our book.
'GET /books/{id}' => [BookController::class, 'show']public function show(Request $request): Response
{
$book = $this->books->find((int) $request->attribute('id'));
if ($book === null) {
return Response::html('<h1>Not found</h1>', 404);
}
return $this->view('books/show.php', ['book' => $book]);
}The route knows the address and action. The controller converts the identifier, asks the repository for the book, handles absence, then chooses a view. The view receives a book ready to display.
Read the controller line by line.
1show receives the request already recognized by the router.
3The URL identifier is converted to an integer, then passed to the repository.
5The controller accounts for a missing book.
6It then selects an explicit 404 response.
9Otherwise, it gives the book to the view; it does not build HTML itself.
Recognize a misplaced responsibility.
SQL inside a viewThe view then knows storage details and becomes hard to test.
HTML inside a modelThe model can no longer serve an API or another presentation.
Everything in the controllerBusiness rules become long, duplicated, and hard to reuse.
MVC is not alone: three complementary roles.
As the application grows, some work fits neither a model, view, nor controller. It receives a precise name instead of making controllers artificially large.
Performs a business operation combining several steps, such as borrowing a book and sending confirmation.
Retrieves or stores data without exposing storage details to the controller.
Runs before or after the controller for a shared rule: authentication, security, or logging.
Checks that received information exists and follows the expected format.
A simple rule for deciding where code belongs.
RouteControllerModel / serviceViewMiddlewareWhy not put everything in one file?
One file feels faster: read the URL, query the database, and write HTML in one place.
Rules get copied, tests require a whole page, and a visual change risks touching data. MVC pays off when the second feature arrives.
BEGINNER QUESTIONS
You may be wondering…
Is MVC mandatory for every small program?
No. A very short exercise can fit in one file. Separation becomes useful when a project has several pages or rules.
Is a model always a database table?
No. It mainly represents domain data or a rule. It can exist before any database.
Does a view always contain HTML?
In a classic application, often yes. In AML View, it can be described with PHPAML View's PHP components.
CHECK YOUR UNDERSTANDING
Who should verify that a discount is allowed: the view or business domain?
Show the answer
The business domain, usually in a model or service. The view only presents the result.
YOUR TURN
For borrowing a book, assign these tasks: check availability, read the URL identifier, display the Borrow button.
Show a hint
Model/service: availability; controller: identifier; view: button.
SUMMARY
What to remember
- The model owns data and business rules.
- The view presents.
- The controller coordinates.
- Good separation reuses the same logic for HTML or JSON.