if (strpos($_SERVER['REQUEST_URI'], '/about') !== false) { require_once('../about.php'); } elseif (strpos($_SERVER['REQUEST_URI'], '/contact') !== false) { require_once('../contact.php'); }
$dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r) { $r->addRoute('GET', '/users', 'getUsers'); $r->addRoute('GET', '/users/{id:\d+}', 'getUserById'); $r->addRoute('GET', '/posts', 'getPosts'); $r->addRoute('GET', '/posts/{id:\d+}', 'getPostById'); }); $routeInfo = $dispatcher->dispatch($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']); switch ($routeInfo[0]) { case FastRoute\Dispatcher::NOT_FOUND: // handle 404 Not Found break; case FastRoute\Dispatcher::METHOD_NOT_ALLOWED: $allowedMethods = $routeInfo[1]; // handle 405 Method Not Allowed break; case FastRoute\Dispatcher::FOUND: $handler = $routeInfo[1]; $vars = $routeInfo[2]; // call handler function with dynamic arguments call_user_func_array($handler, $vars); break; }Package Library: FastRoute is a popular PHP package for routing, as it is lightweight and optimized for high-performance applications.