A tiny front-controller router

Send every request to index.php and pick the page from the URL path. Pair it with an .htaccess rewrite rule.

PHP · A tiny front-controller routerAdvanced
// .htaccess:  RewriteEngine On
//             RewriteCond %{REQUEST_FILENAME} !-f
//             RewriteRule ^ index.php [L]

$path   = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$method = $_SERVER['REQUEST_METHOD'];

$routes = [
    'GET /'            => fn() => print('Home'),
    'GET /about'       => fn() => print('About'),
    'POST /contact'    => fn() => print('Form received'),
];

$handler = $routes["$method $path"] ?? null;
if (!$handler) { http_response_code(404); exit('Page not found'); }
$handler();
routerroutingmvchtaccess