Current Location: Home> Latest Articles> Building a Simple PHP Framework from Scratch

Building a Simple PHP Framework from Scratch

M66 2025-06-29

Building a Simple PHP Framework from Scratch

PHP is a popular server-side scripting language widely used for web application development. Building your own PHP framework can help you better organize and manage your projects, improving code reusability and scalability. In this article, we will walk you through how to build a simple PHP framework from scratch, including relevant code examples.

Creating the Framework Directory Structure

First, create a directory to store the framework files. You can name this directory according to your preference, such as "myframework". Inside this directory, we will create the following structure:

myframework
├── app
│   ├── controllers
│   ├── models
│   └── views
├── config
├── public
├── vendor
└── index.php

In this structure, the `app` directory will store the application code, including controllers, models, and views; the `config` directory is for configuration files; the `public` directory will store public resource files (such as CSS and JavaScript); the `vendor` directory is for third-party libraries; and `index.php` will be the entry point of the framework.

Creating the Base Framework Class

Next, create a file named `Framework.php` in the `app` directory as the base class of the framework. This class will be responsible for handling requests, routing, and dispatching controller methods.

class Framework {
    public function __construct() {
        // Initialize the framework
    }

    public function run() {
        // Handle request and routing
        $route = $this->getRoute();
        $controller = $this->getController($route['controller']);
        $action = $route['action'];

        if (method_exists($controller, $action)) {
            $controller->$action();
        } else {
            // Handle non-existent methods
        }
    }

    private function getRoute() {
        // Parse the request URL to get the controller and method
        $route = $_SERVER['REQUEST_URI'];
        return ['controller' => $controller, 'action' => $action];
    }

    private function getController($controllerName) {
        // Create a controller instance based on the controller name
        $controllerClass = $controllerName . 'Controller';
        $controllerFile = 'app/controllers/' . $controllerClass . '.php';

        if (file_exists($controllerFile)) {
            require_once $controllerFile;
            return new $controllerClass;
        } else {
            // Handle non-existent controllers
        }
    }
}

In the above code, the `__construct()` method is used to initialize the framework, the `run()` method handles requests and dispatches controller methods, `getRoute()` parses the request URL to get the controller and action, and `getController()` creates a controller instance based on the controller name.

Creating Controllers

Next, create a file named `ExampleController.php` in the `app/controllers` directory as an example controller.

class ExampleController {
    public function index() {
        // Handle home page logic
    }

    public function about() {
        // Handle about page logic
    }

    // Other methods...
}

In this code, we created a class named `ExampleController` and defined the `index()` and `about()` methods as examples of controller methods.

Creating Views

In the `app/views` directory, create a file named `index.php` as an example view. In this view file, you can write HTML and PHP code to build the page.

<!DOCTYPE html>
<html>
<head>
    <title>My Framework</title>
</head>
<body>
    <h1>Welcome to my framework!</h1>
</body>
</html>

The above code shows a simple HTML view, which will serve as the front-end page for the framework.

Creating the Entry File

In the root directory of the framework, create a file named `index.php` and include the framework class, create a framework object, and run the framework.

require_once 'app/Framework.php';

$framework = new Framework();
$framework->run();

Now, by visiting `http://yourdomain.com`, you can see the example view.

Conclusion

Through the above steps, we've successfully built a simple PHP framework from scratch. This is a basic framework, and you can expand and customize it according to your needs. Building your own PHP framework helps improve code maintainability and scalability, while also deepening your understanding of framework design principles. I hope this article is helpful to you!