What are some best practices for structuring URLs and handling routing in PHP applications?

When structuring URLs and handling routing in PHP applications, it is important to follow a clean and organized approach to ensure maintainability and scalability. One common practice is to use a front controller pattern, where all requests are directed through a single entry point that then routes the request to the appropriate controller based on the URL. Additionally, using a routing library or framework can help simplify the process of defining routes and handling different request methods.

// index.php (front controller)
$request_uri = $_SERVER['REQUEST_URI'];

// Define routes
$routes = [
    '/' => 'HomeController@index',
    '/about' => 'AboutController@index',
    '/contact' => 'ContactController@index',
];

// Route the request
if (array_key_exists($request_uri, $routes)) {
    list($controller, $method) = explode('@', $routes[$request_uri]);
    require_once 'controllers/' . $controller . '.php';
    $controllerInstance = new $controller();
    $controllerInstance->$method();
} else {
    // Handle 404 error
    header("HTTP/1.0 404 Not Found");
    echo '404 - Page not found';
}