Home > Article > Backend Development > Creating a simple page router in PHP
First, We will create index.php, router.php, and .htaccess files.
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php [L,QSA]
This code will show a 404 response if the user enters the url for accessing router.php directly.
<?php if (basename($_SERVER['PHP_SELF']) == basename(__FILE__)) { http_response_code(404); die(); }
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.
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.
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.
<?php if (basename($_SERVER['PHP_SELF']) == basename(__FILE__)) { http_response_code(404); die(); } class Page { protected static bool $Return_404 = true; protected static function Return_404(): void { (file_exists("./Pages/404.php")) ? require_once "./Pages/404.php" : http_response_code(404); } 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; } } class Router extends Page { 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(); } } class Routes { public static array $Route = array( "" => "Pages/home.php", "/about" => "Pages/about.php", ); }
In the index.php, we will use the "Run" function from the Router class.
<!DOCTYPE html> <html lang="en"> <?php require_once "./router.php"; ?> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Router</title> </head> <body> <?php Router::Run(); ?> </body> </html>
All the HTML code from the pages will be in the "body" tag.
Finally, create home.php, about.php, and 404.php in the Pages folder.
home.php
<h1>Home page</h1>
about.php
<h1>about page</h1>
404.php
<h1>404 page</h1>
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:
The above is the detailed content of Creating a simple page router in PHP. For more information, please follow other related articles on the PHP Chinese website!