Back to overview

Building a Routing System in PHP from Scratch

September 25, 2023

If you’re just at the beginning of your journey in PHP development, chance is that you use complete file names in the URL to navigate your application, like server/contact.php. No worries, we all started that way, and it’s how we learn.

Today, I want you to improve how you navigate files in your application. We’ll be talking about routing, as it’s very crucial in any modern application. It’s a step forward in professional PHP development. A routing system simply maps an HTTP request to a request handler (function or method). In other words, it defines how we navigate or access different parts of your app without the need to type the file name. This is done by creating or setting routes (or paths). E.g.: the route server/contact lets us access the contact.php file.

How routing works

Everything happens around a router. This later is the entry file of the app, and we name it index.php. Inside the file, we define the routing system thanks to switch or match. Last but not least, we must redirect all requests to the router. This is done in the configuration file of your PHP server.

Before moving forward, let’s set up our project.

mkdir php-routing & cd php-routing
touch index.php .htaccess 

We’ll create views later. .htaccess is a directory-level Apache configuration file. You don’t need it if you use an Nginx server.

Redirect all HTTP requests to the router

In the .htaccess file, add the directives below

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php

First, activate the Apache server’s runtime rewriting engine. The next directive (RewriteCond) limits access to physical files and lastly, all the upcoming requests are redirected to the index.php file.

If it happens that your site or app is not at the root of our server (or if we don’t have a virtual host), your .htaccess should look like this:

RewriteEngine On
RewriteBase /folder/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /folder/index.php [L]

Replace /folder/ with the name of the folder containing your site.

Let’s see how you can redirect with an Nginx server.

If your PHP server is an Nginx server, you need to update the main configuration file named nginx.conf by default.

location / {
        try_files $uri $uri/ /index.php?$args;

Reload the server after the modification.

Should you miss where the configuration file is placed, try searching in one of the following directories: /usr/local/nginx/conf , /etc/nginx or /usr/local/etc/nginx.

Create the routing system

We know how routing works, and we are even sending all requests to the router. Now is the time to write the code for the router.

In index.php add the code below


$request = $_SERVER['REQUEST_URI'];

switch ($request) {

    case '':
    case '/':
        require __DIR__ . '/views/home.php';

    case '/views/users':
        require __DIR__ . '/views/users.php';

    case '/contact':
        require __DIR__ . '/views/contact.php';

        require __DIR__ . '/views/404.php';

First, we get the user request, then we call the appropriate page in the views directory. In case the path is not found in the server, we call the 404.php page. I use both '' and '/' to match as well as

Create the views

Using the terminal, create the views directory in the root of the project and cd into it.

mkdir views
cd views

Create 4 files in the folder as shown below.

touch home.php users.php contact.php 404.php

Let’s just put some content in each file.

// home.php
<p>Welcome in my app.</p>
// users.php
<p>List of our users.</p>
// contact.php
<h1>Conct us</h1>
<p>Getting in touch is easy. Just email us</p>
// 404.php
<p>You've reached the end of Internet.</p>

Final thoughts

In this tutorial, we learned how to create a basic routing system from scratch. Let’s recall the steps to follow:

  • create a file name index.php in the root of the project. This is the router for your app
  • redirect all incoming requests to the router. This is done in the configuration file of your server
  • create the routing system with switch statement in the router

That’s all.

Happy coding!

All the content in this website are licensed under the CC-BY-SA license, unless otherwise specified