What Are We Building?
Welcome to our project-based module! Over the next few lessons, we will build a simple but powerful MVC (Model-View-Controller) framework from scratch. This is the single most important concept to learn to understand how modern PHP frameworks like Laravel, Symfony, or CodeIgniter work behind the scenes. Our goal is to create a basic blog application using our own custom framework.
What is MVC? A Simple Analogy
MVC is a design pattern that separates an application into three interconnected parts:
- Model: Manages the data and business logic. It's the "kitchen" of our application, responsible for interacting with the database (e.g., getting posts, saving users).
- View: The user interface. It's the "menu" that the customer sees. It displays the data prepared by the Controller but doesn't have any logic of its own. In our case, these will be our HTML files.
- Controller: Acts as the intermediary. It's the "waiter." It takes requests from the user, asks the Model for the necessary data, and then tells the View which menu to display with that data.
Step 1: The Folder Structure
First, create a new folder for our project. Inside, create the following folders and files. This structure separates our public files from our application logic, which is a crucial security practice.
mvc-project/
├── app/
│ ├── Controllers/
│ ├── Models/
│ └── Core/
├── public/
│ ├── css/
│ └── index.php
├── views/
├── vendor/
└── composer.json
app/
: Will contain all our core PHP classes (Controllers, Models, etc.).public/
: The only folder accessible from the web browser. Ourindex.php
(the "Front Controller") will live here.views/
: Will contain our HTML template files.
Step 2: Setting Up Composer and Autoloading
Create the composer.json
file in your project root and add our PSR-4 autoloading configuration. This will automatically load all our classes from the app/
directory.
File: composer.json
{
"name": "your-name/mvc-project",
"description": "A simple PHP MVC framework.",
"autoload": {
"psr-4": {
"App\\": "app/"
}
}
}
After creating the file, open your terminal in the mvc-project
directory and run the command composer install
. This will generate the vendor/autoload.php
file.
Step 3: The Entry Point (index.php)
All web requests will be directed to this single file. Its only job, for now, is to load the Composer autoloader.
File: public/index.php
<?php
// This is the single entry point for our entire application.
// Load the Composer autoloader to handle our classes.
require_once __DIR__ . '/../vendor/autoload.php';
echo "MVC Project is running!";
Your Mission
- Create the folder structure as shown above.
- Create the
composer.json
file and runcomposer install
. - Create the
public/index.php
file. - Configure your web server (Apache/Nginx) to point the domain's "document root" to the
public/
folder. When you visit your site, you should see the message "MVC Project is running!".
Once you've completed this setup, you have the foundational skeleton of a modern PHP application!