"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Creating a simple page router in PHP

Creating a simple page router in PHP

Published on 2024-11-07
Browse:412

Create files

First, We will create index.php, router.php, and .htaccess files.

Redirect all requests to index.php in .htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L,QSA]

Prevent from direct access for router.php

This code will show a 404 response if the user enters the url for accessing router.php directly.

Add Page class in router.php

class Page
{
  protected static bool $Return_404 = true;
}

Create a static boolean variable for returning the 404 page. We will set it to true as default.
Now we add a function for the 404 page.

protected static function Return_404(): void
    {
       (file_exists("./Pages/404.php")) ?  require_once "./Pages/404.php" : http_response_code(404);
    }

Here, I put a 404 page inside the Pages folder. You can put it in any folder you want.

We will also add the "File" function.

protected static function File(string $file): string
{
    if (!empty($file)) {
        (str_contains($file, "?")) ? $file = strtok($file, '?') : $file;
        ($file[strlen($file) - 1] === "/") ? $file = rtrim($file, "/") : $file;
    }
    return $file;
}

This function will check if the request url contains a query string or ending with "/" and remove it.
I use "strok" string function to get the string values before "?". I know "strok" isn't meant to be used like this but it works and saves me from doing complex algorithms unnecessarily. I use "rtrim" string function to remove the "/" if it is contained at the last of the string.

Add Routes class in router.php

class Routes
{
    public static array $Route = array(
        "" => "Pages/home.php",
        "/about" => "Pages/about.php",
    );
}

Here, I create a static array for storing routings.
This array contains "Requested url" => "File location".
I put all the page files in the Pages folder. You can put them anywhere you want.

Add Router class in router.php

The Router class will extend from the Page class that we already created above.
Note that "" => "Pages/home.php", is for the home page.

class Router extends Page
{

}

Now, we will write the function that checks whether the requested file exists in the Router class.

   public static function Run(): void
    {
        $requested_file = self::File($_SERVER["REQUEST_URI"]);
        foreach (Routes::$Route as $request => $file) {
            if ($requested_file === $request) {
                if (file_exists($file)) {
                    self::$Return_404 = false;
                    require $file;
                } else echo "Error";
            }
        }
        if (self::$Return_404) self::Return_404();
    }

This function checks the requested file is in the $Route array first. If it exists, the static boolean $Return_404 is set to false so that the 404 page will not show and get the page file. If not exist, this will return the 404 page.
If it exists but there is no file in the Pages folder, the function will echo "Error". You can show the 404 page instead of echo "Error" here.

The final router.php file will look like this.

 $file) {
            if ($requested_file === $request) {
                if (file_exists($file)) {
                    self::$Return_404 = false;
                    require $file;
                } else echo "Error";
            }
        }
        if (self::$Return_404) self::Return_404();
    }
}
class Routes
{
    public static array $Route = array(
        "" => "Pages/home.php",
        "/about" => "Pages/about.php",
    );
}

Index.php

In the index.php, we will use the "Run" function from the Router class.


Router

All the HTML code from the pages will be in the "body" tag.

Create pages

Finally, create home.php, about.php, and 404.php in the Pages folder.

home.php

Home page

about.php

about page

404.php

404 page

Check if the code working or not with "xampp" or "PHP server" extension.

If you encounter an error, feel free to tell me. XD

You can also check the github repository at:

Creating a simple page router in PHP Tokigin / page-router

Manual router from php-router repository

Release Statement This article is reproduced at: https://dev.to/kohtet_gintoki/creating-a-simple-page-router-in-php-2hpf?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3