How does the concept of routing work in PHP, and what are some common practices for implementing it effectively?

In PHP, routing is the process of mapping URLs to specific actions or content within a web application. One common practice for implementing routing effectively is to use a front controller pattern, where all requests are directed to a single PHP file that handles the routing logic based on the requested URL.

// index.php

// Define routes
$routes = [
    '/' => 'home.php',
    '/about' => 'about.php',
    '/contact' => 'contact.php',
];

// Get requested URL
$requestUri = $_SERVER['REQUEST_URI'];

// Match route
if (array_key_exists($requestUri, $routes)) {
    include $routes[$requestUri];
} else {
    echo '404 Not Found';
}