In this tutorial, you will learn how to build a PHP Model-View-Controller (MVC) framework from scratch.
Create a new directory for your project and navigate to it using the command line:
mkdir php-mvc-framework
cd php-mvc-framework
Initialize a new Composer project:
composer init
Follow the prompts to set up your project. When asked for dependencies, leave it empty for now.
Create the following directory structure for your project:
src/
Controllers/
Models/
Views/
Create a new file named Router.php in the src/ directory. This file will contain the main routing logic for your framework.
<?php
namespace MVC;
class Router {
protected $routes = [];
public function addRoute($route, $controller, $action) {
$this->routes[$route] = ['controller' => $controller, 'action' => $action];
}
public function dispatch($uri) {
if (array_key_exists($uri, $this->routes)) {
$controller = $this->routes[$uri]['controller'];
$action = $this->routes[$uri]['action'];
$controller = new $controller();
$controller->$action();
} else {
throw new \Exception("No route found for URI: $uri");
}
}
}
Create a new file named Controller.php in the src/ directory. This file will contain the base controller class that all other controllers will extend.
<?php
namespace MVC;
class Controller {
protected function render($view, $data = []) {
extract($data);
include "Views/$view.php";
}
}
Create a new file named User.php in the src/Models/ directory. This model will represent a user in your application.
<?php
namespace MVC\Models;
class User {
public $name;
public $email;
public function __construct($name, $email) {
$this->name = $name;
$this->email = $email;
}
}
Create a new file named UserController.php in the src/Controllers/ directory. This controller will handle user-related actions.
<?php
namespace MVC\Controllers;
use MVC\Controller;
use MVC\Models\User;
class UserController extends Controller {
public function index() {
$users = [
new User('John Doe', 'john@example.com'),
new User('Jane Doe', 'jane@example.com')
];
$this->render('user/index', ['users' => $users]);
}
}
Create a new file named index.php in the src/Views/user/ directory. This view will display a list of users.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>User List</title>
</head>
<body>
<h1>User List</h1>
<ul>
<?php foreach ($users as $user): ?>
<li><?= $user->name ?> (<?= $user->email ?>)</li>
<?php endforeach; ?>
</ul>
</body>
</html>
Create a new file named routes.php in the src/ directory. This file will define the routes for your application.
<?php
use MVC\Router;
use MVC\Controllers\UserController;
$router = new Router();
$router->addRoute('/', UserController::class, 'index');
Create a new file named index.php in the root directory of your project. This file will serve as the entry point for your application.
<?php
require 'vendor/autoload.php';
$uri = $_SERVER['REQUEST_URI'];
$router = require 'src/routes.php';
$router->dispatch($uri);
Run the built-in PHP web server to test your application:
php -S localhost:8000
Visit http://localhost:8000 in your web browser to see the user list.