Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🔴 Advanced  ·  Lesson 39

MVC Architecture in PHP

In this lesson: Understand MVC architecture in PHP with folder structure, router and a tiny working example — explained simply.

What is MVC

MVC stands for Model, View, Controller — a way of organising an app by splitting it into three parts, each with one job. Instead of one giant file mixing database code, logic and HTML, MVC keeps them separate and tidy. Almost every modern PHP framework is built on this pattern.

💡 Simple picture: Model = data, View = looks, Controller = the brain that connects them.

Model (data)

The Model handles data — talking to the database and enforcing rules.

<?php
  class UserModel {
      public function getUser($id) {
          // fetch from database (via PDO)
          return ["id" => $id, "name" => "Aman"];
      }
  }
?>

Only the Model touches the database. Everything about "what the data is and how to get it" lives here, in one place.

View (display)

The View is what the user sees — the HTML, filled with data.

<!-- user_view.php -->
<h1>Welcome <?php echo $name; ?></h1>

The View only displays; it contains no database code and little logic. This means a designer can edit the look without touching the app's brains.

Controller (logic)

The Controller is the go-between: it takes the request, asks the Model for data, and picks a View.

<?php
  class UserController {
      public function show($id) {
          $model = new UserModel();
          $user = $model->getUser($id);
          $name = $user["name"];
          include "user_view.php";   // show the View
      }
  }
?>

The Controller holds the "what to do" logic but no SQL and no HTML — it coordinates the Model and the View.

How a request flows

  1. A user visits a URL like /user/5.
  2. The Controller receives it and reads the id (5).
  3. It asks the Model for user 5's data.
  4. It passes that data to the View.
  5. The View renders HTML, which is sent to the browser.

Each part did its own job — data, coordination, display — and stayed out of the others' way.

Why use MVC

  • Separation. Change the layout without touching database code.
  • Teamwork. A designer edits Views while a developer edits Models.
  • Reuse. One Model can feed many Views.
  • Maintainable. Bugs are easier to find when each part has one job.

Common mistakes

  • Putting SQL in the Controller or View. Database code belongs only in the Model.
  • Heavy logic in the View. Views should mostly display, not decide.
  • A "fat" Controller doing everything. Move data work to the Model.
  • Thinking you need a framework. MVC is a pattern you can apply in plain PHP.

Summary

  • MVC splits an app into Model, View and Controller.
  • Model = data and database; View = the HTML the user sees.
  • Controller = the logic that connects Model and View.
  • A request flows Controller → Model → View → browser.
  • MVC keeps big projects organised and easy to maintain.

Next, load classes cleanly with autoloading, or organise them with namespaces.

Frequently Asked Questions

What does MVC stand for?
MVC stands for Model, View, Controller — three parts that split an app by responsibility. The Model handles data, the View handles what the user sees, and the Controller handles the logic that ties them together.
What is the job of each MVC part?
The Model talks to the database and holds data rules. The View is the HTML the user sees. The Controller receives the request, asks the Model for data, and picks a View to show. Each part has one clear job.
Why use MVC instead of mixing everything?
Mixing SQL, logic and HTML in one file becomes messy and hard to change. MVC separates them so you can edit the layout without touching the database code, and vice versa. It makes big projects maintainable.
Do I need a framework to use MVC?
No. MVC is a pattern, not a tool — you can organise plain PHP into models, views and controllers yourself. Frameworks like Laravel just give you a ready-made MVC structure and helpers.
Where does the database code go in MVC?
In the Model. The Model is the only part that talks to the database. Controllers ask the Model for data and never run SQL directly, which keeps data logic in one place.
← Back to PHP Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicLogin & Registration System Your progress: 39 / 50 (78%) · Lesson 40 / 50 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free