// Basic example of the PHP routing code $url = $_SERVER['REQUEST_URI']; if ($url == '/') { include('home.php'); } else if ($url == '/about') { include('about.php'); } else if ($url == '/contact') { include('contact.php'); } else { include('404.php'); }
// Example of using a package library for PHP routing use \Psr\Http\Message\ServerRequestInterface as Request; use \Psr\Http\Message\ResponseInterface as Response; require 'vendor/autoload.php'; $app = new \Slim\App; $app->get('/', function (Request $request, Response $response) { $response->getBody()->write("Welcome to the homepage"); return $response; }); $app->get('/about', function (Request $request, Response $response) { $response->getBody()->write("About us page"); return $response; }); $app->get('/contact', function (Request $request, Response $response) { $response->getBody()->write("Contact us page"); return $response; }); $app->run();This code is using a package library called Slim to handle the routing. Slim is a micro framework that allows for easy and organized routing of PHP web applications. This example code sets up routes for the homepage, about page, and contact page using the get() method, and returns the corresponding response messages. In conclusion, the use of a package library such as Slim can make routing easier and more organized while providing additional functionality such as middleware and error handling.