request = $request; $this->response = $response; $this->args = $args; $this->routeContext = RouteContext::fromRequest($this->request); $this->view = Twig::fromRequest($request); return $this->handle($request, $response); } /** * Implement this method for handling the request. * * @return Response */ abstract public function handle(): Response; /** * Render the given Template. * * @param string $templateName The name of the template * @param array $data The data for the template * @return Response */ public function render(string $templateName, array $data = []): Response { return $this->view ->render($this->response, $templateName, $data) ->withHeader('Content-Type', 'text/html'); } /** * @param string $html The raw Twig HTML to render * @param array $data The data to injext into the HTML * @return Response */ public function renderInline(string $html, array $data = []): Response { $renderedTemplate = $this->view->fetchFromString($html, $data); $this->response->getBody()->write($renderedTemplate); return $this->response->withHeader('Content-Type', 'text/html'); } /** * @param array|object $data The data to encode as JSON * @return Response */ public function json(array|object $data): Response { $encodedData = json_encode($data); $this->response->getBody()->write($encodedData); return $this->response->withHeader('Content-Type', 'application/json'); } }