/** * Converts an HtmlFragment into an HtmlPage. * * @param \Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent $event * The Event to process. */ public function onHtmlFragment(GetResponseForControllerResultEvent $event) { $fragment = $event->getControllerResult(); if ($fragment instanceof HtmlFragment && !$fragment instanceof HtmlPage) { $page = $this->fragmentRenderer->render($fragment); $event->setControllerResult($page); } }
/** * @param $title * The page title of the response. * @param $body * The body of the error page. * @param $response_code * The HTTP response code of the response. * @return Response * An error Response object ready to return to the browser. */ protected function createResponse($title, $body, $response_code) { $fragment = new HtmlFragment($body); $fragment->setTitle($title); $page = $this->fragmentRenderer->render($fragment, $response_code); return new Response($this->htmlPageRenderer->render($page), $page->getStatusCode()); }
/** * Processes a NotFound exception into an HTTP 404 response. * * @param \Symfony\Component\Debug\Exception\FlattenException $exception * The flattened exception. * @param \Symfony\Component\HttpFoundation\Request $request * The request object that triggered this exception. * * @return \Symfony\Component\HttpFoundation\Response * A response object. */ public function on404Html(FlattenException $exception, Request $request) { watchdog('page not found', String::checkPlain($request->attributes->get('_system_path')), array(), WATCHDOG_WARNING); // Check for and return a fast 404 page if configured. $config = \Drupal::config('system.performance'); $exclude_paths = $config->get('fast_404.exclude_paths'); if ($config->get('fast_404.enabled') && $exclude_paths && !preg_match($exclude_paths, $request->getPathInfo())) { $fast_paths = $config->get('fast_404.paths'); if ($fast_paths && preg_match($fast_paths, $request->getPathInfo())) { $fast_404_html = $config->get('fast_404.html'); $fast_404_html = strtr($fast_404_html, array('@path' => String::checkPlain($request->getUri()))); return new Response($fast_404_html, 404); } } // @todo Remove dependency on the internal _system_path attribute: // https://www.drupal.org/node/2293523. $system_path = $request->attributes->get('_system_path'); $path = $this->container->get('path.alias_manager')->getPathByAlias(\Drupal::config('system.site')->get('page.404')); if ($path && $path != $system_path) { // @todo Um, how do I specify an override URL again? Totally not clear. Do // that and sub-call the kernel rather than using meah(). // @todo The create() method expects a slash-prefixed path, but we store a // normal system path in the site_404 variable. if ($request->getMethod() === 'POST') { $subrequest = Request::create($request->getBaseUrl() . '/' . $path, 'POST', array('destination' => $system_path, '_exception_statuscode' => 404) + $request->request->all(), $request->cookies->all(), array(), $request->server->all()); } else { $subrequest = Request::create($request->getBaseUrl() . '/' . $path, 'GET', array('destination' => $system_path, '_exception_statuscode' => 404), $request->cookies->all(), array(), $request->server->all()); } $response = $this->container->get('http_kernel')->handle($subrequest, HttpKernelInterface::SUB_REQUEST); $response->setStatusCode(404, 'Not Found'); } else { $page_content = array('#markup' => $this->t('The requested page "@path" could not be found.', array('@path' => $request->getPathInfo())), '#title' => $this->t('Page not found')); $fragment = $this->createHtmlFragment($page_content, $request); $page = $this->fragmentRenderer->render($fragment, 404); $response = new Response($this->htmlPageRenderer->render($page), $page->getStatusCode()); return $response; } return $response; }