routes['#^' . $regex . '$#'] = $handler; return $this; } public function resolve(string $uri): array { $path = parse_url($uri, PHP_URL_PATH); $path = rtrim($path, '/') ?: '/'; foreach ($this->routes as $regex => $handler) { if (preg_match($regex, $path, $matches)) { array_shift($matches); return [$handler, $matches]; } } return ['pages/404.php', []]; } public function dispatch(): void { $uri = $_SERVER['REQUEST_URI'] ?? '/'; [$handler, $params] = $this->resolve($uri); $GLOBALS['routeParams'] = $params; require __DIR__ . '/../' . $handler; } }