Table of contents
Hello everyone, in this tutorial, I will walk you through creating a simple routing logic in PHP.
Prerequisite
Basic knowledge of PHP is required.
Basic knowledge of HTML.
Internet is also required only to have access to the source code.
Basic knowledge of Git and GitHub if you want the source code.
Have a source code editor like vscode installed on your system.
Introduction
The traditional way to create a web project with links to other pages in either HTML or PHP is to create the file if the extension ends with .php
or .html
, you go to your browser and type http//localhost/about.php
or http://localhost/about.html
Well, this is great and looks good, how about we make it looks better so that it will save users or you from always typing the extension part just like most frameworks does?
You or your users should be able to make requests using /about which is quite easier to type and looks much prettier.
Example Code
The example in this tutorial uses PHP, so you should be familiar with PHP, of course, you don't need to be an expert, just know about PHP. I won't be using Xampp in this tutorial but will be using the built-in PHP testing development server.
<?php
$method = $_SERVER["REQUEST_METHOD"];
$uri = $_SERVER['REQUEST_URI'];
switch ($method | $uri) {
case $method == "GET" && $uri == '/about':
require "about.html";
break;
case $method == "GET" && $uri == '/contact':
require "contact.html";
break;
case $method == "POST":
echo $_POST['full_name'];
break;
default:
http_response_code(404);
// You can include your error page here
echo "<h2>The page you are requesting does not exist</h2>";
}
Explanation of code
The opening <?php
tag is to tell the server that it should treat the text in this document as a PHP script
The second line $method = $_SERVER["REQUEST_METHOD"];
is a variable I set to store which method the user is using to request the page, this is stored in a variable so that we can reuse it. There are various request method HTTP
the verb that is in use, but in our case in this tutorial, we will be using the GET
and POST
HTTP
verb
The third line $uri = $_SERVER["REQUEST_URI"];
is a variable set to check the URI the user typed in the browser search bar to send the requested page.
The fifth line is the main logic where everything works together using the switch statement in PHP. To check which request method or request URI the user typed in their browser search bar, we make use of switch($method | $uri){}
.
Inside the curly braces, the case we are using is also to check the URI and method then we use the require function to load the page, at then we use the break function to terminate the script so that we won't see the default page if the page exists.
The default function is used to send an error page if the page is not found. For example, if I make a request using http://localhost:8000
it will throw an error because I don't have a page for that, but if the page does exist, it will load the page to the browser.
Conclusion
You can find the source code for this article on my GitHub repo.
Thanks for reading, you can like and share this tutorial if you find it interesting.