routes[$path] = [ 'controller' => $controller, 'action' => $action, ]; } public function dispatch(string $uri): void { // Normalize: strip query string and trailing slash $path = parse_url($uri, PHP_URL_PATH); $path = rtrim($path, '/') ?: '/'; // Direct match if (isset($this->routes[$path])) { $this->execute($this->routes[$path]); return; } // Legacy .html redirect (301) if (preg_match('#^/(impressum|datenschutz)\.html$#', $path, $m)) { header('Location: /' . $m[1], true, 301); exit; } // 404 http_response_code(404); echo '

404 – Seite nicht gefunden

'; echo '

Zurück zur Startseite

'; } private function execute(array $route): void { $controllerClass = $route['controller']; $action = $route['action']; if (!class_exists($controllerClass)) { throw new \RuntimeException("Controller {$controllerClass} nicht gefunden."); } $controller = new $controllerClass(); if (!method_exists($controller, $action)) { throw new \RuntimeException("Action {$action} in {$controllerClass} nicht gefunden."); } $controller->$action(); } }