Current Location: Home> Latest Articles> How to Build a Custom Framework in PHP: Create Your Own PHP Framework from Scratch

How to Build a Custom Framework in PHP: Create Your Own PHP Framework from Scratch

M66 2025-06-16

How to Build a Custom Framework in PHP

A custom framework is a common requirement in web development. By building your own framework, developers can better meet project requirements and improve development efficiency. This article will show you how to build a simple custom framework in PHP.

1. Framework Structure

A typical PHP framework should include the following components:

  1. Router: Responsible for mapping URLs to controllers and actions (methods).
  2. Controller: Handles the request, calls the model to fetch data, then renders the view and returns a response.
  3. Model: Handles interactions with the database and performs CRUD operations.
  4. View: Displays data and shows results to the user.
  5. Core Class: Contains core framework functionalities, such as configuration parsing and error handling.

Next, we will implement a custom framework step by step based on the above structure.

2. Implementing the Router

The router determines which controller and action correspond to a given URL.

class Router {
    protected $controller = 'DefaultController';
    protected $action = 'indexAction';

    public function handleRequest() {
        $url = $_SERVER['REQUEST_URI'];

        // Parse URL to get controller and action
        $parts = explode('/', $url);
        if (isset($parts[1]) && !empty($parts[1])) {
            $this->controller = ucfirst($parts[1]) . 'Controller';
        }

        if (isset($parts[2]) && !empty($parts[2])) {
            $this->action = $parts[2] . 'Action';
        }

        // Create controller object and call the corresponding method
        $controller = new $this->controller();
        $controller->{$this->action}();
    }
}

3. Implementing the Controller

The controller receives and processes the request, then calls the model and view to complete the operation.

class DefaultController {
    public function indexAction() {
        echo 'Hello, welcome to my custom framework!';
    }
}

4. Implementing the Model

The model handles interactions with the database and performs CRUD operations. In this example, we will not perform database operations, but simply provide a basic method.

class UserModel {
    public function getAllUsers() {
        return [
            ['id' => 1, 'name' => 'Alice'],
            ['id' => 2, 'name' => 'Bob'],
            ['id' => 3, 'name' => 'Charlie'],
        ];
    }
}

5. Implementing the View

The view is responsible for displaying the data and showing it to the user.

class View {
    public function render($data) {
        foreach ($data as $item) {
            echo 'ID: ' . $item['id'] . ', Name: ' . $item['name'] . '<br>';
        }
    }
}

6. Integrating All Components into the Entry File

require_once 'Router.php';
require_once 'Controller.php';
require_once 'Model.php';
require_once 'View.php';

$router = new Router();
$router->handleRequest();

7. Running the Framework

Save the above code as index.php and place it in the root directory of your web server. You can then visit http://localhost/ to see the output.

For example, visiting http://localhost/user/getAll will show the following results:

ID: 1, Name: Alice
ID: 2, Name: Bob
ID: 3, Name: Charlie

Summary

This article explains how to build a simple custom framework in PHP. A fully-fledged framework typically includes a router, controller, model, view, and core classes to handle requests and generate responses.

Custom frameworks help developers better meet project requirements and increase development efficiency. We hope this article has helped you understand how to build your own PHP framework.