Trusted answers to developer questions

How to create a basic PHP router

Get Started With Machine Learning

Learn the fundamentals of Machine Learning with this free course. Future-proof your career by adding ML skills to your toolkit — or prepare to land a job in AI or Data Science.

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 note
  • courses.php: list of courses
  • authors.php: list of authors
  • aboutus.php: about the platform

How beginners navigate files

Imagine 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.

How professionals navigate files

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:

  • redirect all HTTP requests to index.php (the router)
  • create a routing system with switch

Redirect all HTTP requests to the router

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.

Create a routing system with switch

Our routing system will work as follows:

  • get the user requested path with $_SERVER super global variable
  • require the corresponding page

Code

Add 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.

Create the views

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>

RELATED TAGS

php
router
Did you find this helpful?