routes = new RouteCollection(); $this->addRoute('GET', '/', [Home::class, 'index']); $this->addRoute('GET', '/dashboard', [Home::class, 'dashboard']); // card $this->addRoute('GET', '/card/list', [Card::class, 'listCards']); $this->addRoute('GET', '/card/add', [Card::class, 'addCard']); $this->addRoute('GET', '/card/{id}', [Card::class, 'displayCard']); $this->addRoute('GET', '/card/{id}/edit', [Card::class, 'editCard']); $this->addRoute('POST', '/card/add', [Card::class, 'add']); $this->addRoute('POST', '/card/{id}/edit', [Card::class, 'edit']); $context = new RequestContext(); $context->fromRequest($request); $matcher = new UrlMatcher($this->routes, $context); try { $match = $matcher->match($request->getPathInfo()); $request->attributes->set('route', $match); // run controller function $content = call_user_func($this->routeFunctions[$match['_route']], $request); // set response to new response if ($content instanceof Response) { $response = $content; } // set content directly otherwise else { $response->setContent($content); } } // catch any errors catch (AppException $exception) { $response->setStatusCode($exception->getCode()); $response->setContent($exception->getMessage()); } catch (\Exception $exception) { $response->setStatusCode(Response::HTTP_BAD_REQUEST); $response->setContent($exception->getMessage()); } $response->send(); } /** * @param string|array $methods * @param string $path * @param callable $function */ public function addRoute(string|array $methods, string $path, array $function) { $name = "$function[0]::$function[1]"; $this->routes->add($name, new Route($path, methods: (array)$methods)); $this->routeFunctions[$name] = $function; } }