PHP Tutorial: How to make your own PHP framework [part 1 of 2]

13th August 2022, 09:56

We’ve all been there; “how does a framework work?”, “what is the purpose of a framework?” or maybe even “how do I make my own framework?”.

In this tutorial we’ll teach you to make your own PHP framework in the simplest way possible. The things we’ll be learning are:

  • How to create an Application class
  • How url-routing works
  • How view-rendering is handled

Before starting however, it might be useful to have a server like Wamp installed so you can locally host a webserver.

The application

To start building the framework, we’ll need a class that handles the startup and load of the things we need for the framework.

We can begin by creating an index.php and adding the following code:

<?php 

class Application
{
    public function __construct()
    {
        $this->request = new Request();
        $this->routes = [];
    }
}

Very important is to note the beginning of the file: <?php, this marks the beginning of PHP code.

This is Application class for our framework. The __construct method is called every time a new instance of Application is created. You might be saying; “But wait, what’s Request?” and you’re absolutely right. .

In the same index.php file, after the Application, add the following Request-class:

class Request
{   
    public function __construct() {
        $this->url = parse_url($_SERVER['REQUEST_URI']);
        $this->queryParams = $_GET;

        if ($_POST) {
            $this->postParams = $_POST;
        }

        if ($_FILES) {
            $this->files = $_FILES;
        }
    }
}

That’s a lot of code right? This __construct method is a lot larger than the Application‘s. That’s because we’re already (sort of) loading the requested URL in our Request class. That means every time we create a new Request, the current url, query-parameters, post-body and even the uploaded files are added to this class.

Next, we’re going to add some more methods to our Request class. Place the method‘s below anywhere in the Request class and your Request class should look something like this.

class Request
{   
    public function __construct() {
        ...
    }

    public function getPath()
    {
        return $this->url["path"];
    }

    public function getQueryParams()
    {
        return $this->queryParams;
    }

    public function getPostParams()
    {
        return $this->postParams;
    }

    public function getFiles()
    {
        return $this->files;
    }
}

It is recommended to put any “local methods” below the __construct method. This helps organize your code so you always know exactly where to look.

At the bottom of your index.php file, outside of any classes, add the following code:

$app = new Application();
$app->addRoutes([
    "/my-page" => function (Request $request) {
        return $this->render("templates/my-page.php", [
            "title" => "I made this myself!",
            "content" => "Hello from my page!"
        ]);
    }
]);
$app->run();

The code above does the following:

  • First we create a new Application and name it $app
  • After creating $app, we call the method addRoutes with an array of arguments. /my-page is the URL you would like to use and the following function (closure) returns a rendered result.
  • Finally we call the $app->run method, to run the application

Before we conclude the first part of this tutorials, let’s add two more functions to our Application class named addRoutes and run:

class Application
{
    public function __construct()
    {
        ...
    }

    public function addRoutes()
    {

    }

    public function run()
    {
        echo "Hoorah! We finished the first part of the tutorial!";
    }
}

If you run the code (or visit your webserver) you should see the following message:

Hoorah! We finished the first part of the tutorial!

In the next post, we’ll structure our Application, add route-handling and even render some custom templates.

Check out part two here: PHP Tutorial: How to make your own PHP framework [part 2 of 2]


Comments

  1. Hairstyles says:

    Somebody essentially lend a hand to make severely posts I would state. That is the very first time I frequented your web page and so far? I amazed with the research you made to make this particular submit incredible. Excellent task!

  2. Hairstyles says:

    I am extremely impressed together with your writing talents as smartly as with the layout in your blog. Is that this a paid theme or did you customize it yourself? Either way keep up the nice quality writing, it抯 rare to peer a nice weblog like this one today..

  3. Hairstyles says:

    I抎 have to verify with you here. Which is not one thing I normally do! I enjoy reading a put up that may make folks think. Also, thanks for allowing me to comment!

Leave a Reply

Your email address will not be published. Required fields are marked *


Recent posts

PHP Tutorial: How to make your own PHP framework [part 2 of 2]

This is the second part in our “How to make your own PHP framework”-tutorial. Check out the first part of […]

Read more
If-statements

This is a post about if statements and how to use them accordingly (depending on your language of course).

Read more