When you begin your PHP journey, chances are that you will primarily hard code file paths in the URL to access them.
Let’s say you have a project called platform with the following files.
index.php
: welcome notecourses.php
: list of coursesauthors.php
: list of authorsaboutus.php
: about the platformImagine you want to access a list of all authors. What do you do? You type platform.com/authors.php
on the URL.
It works, but what if you decide to change the name of the file one day? You’ll have to fix it everywhere you called this link.
Another issue with this way of directly calling files is that you reveal the project’s internal structure to the world, which is unsafe for security purposes.
Lastly, the URL does not look elegant. For example, calling a URL like platform.com/authors
is more elegant than platform.com/authors.php
.
Let’s see how we can do better.
To do things better, we’ll need to create a routing system, which is a technique that consists of mapping all HTTP requests to a request handler. We will:
index.php
(the router)switch
To redirect all HTTP requests to the router, create a file named .htaccess
on the root of the project and redirect all traffic to index.php
. This is shown below:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php
In our .htaccess
file, we first activate the Apache server’s runtime rewriting engine. We limit access to physical files with Apache RewriteCond
directive. Then, we redirect all upcoming requests to the index.php
file.
.htaccess
is a configuration file for the Apache server. It has no extension, but starts with a dot (.
) for a hidden file.
In case your app/site is not at the root of your server, or if you don’t have a virtual host, you’ll need to create your .htaccess
. This is shown below:
RewriteEngine On
RewriteBase /<folder>/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /folder/index.php [L]
Note: Replace
<folder>
with the name of the folder containing your site.
switch
Our routing system will work as follows:
$_SERVER
super global variableAdd the code below after an index.php
file is created.
<?php$request = $_SERVER['REQUEST_URI'];switch ($request) {case '':case '/':require __DIR__ . '/views/index.php';break;case '/courses':require __DIR__ . '/views/courses.php';break;case '/views/authors':require __DIR__ . '/views/authors.php';break;case '/about':require __DIR__ . '/views/aboutus.php';break;default:http_response_code(404);require __DIR__ . '/views/404.php';break;}
We have defined our four routes plus a default one if a user requests a path that does not exist. In this latter case, we would display a custom Not Found
page.
All our files live in the views
directory.
Create it and place the files:
<h1>Platform</h1><p>Welcome to the platform.</p>
<h1>Courses</h1><p>List of our courses.</p>
<h1>Authors</h1><p>List of our authors.</p>
<h1>About</h1><p>We are leaders in courses publications.</p>
<h1>404</h1><p>You've reached the end of Internet.</p>