/** * Renders an HtmlPage object to a Response. * * @param \Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent $event * The Event to process. */ public function onHtmlPage(GetResponseForControllerResultEvent $event) { $page = $event->getControllerResult(); if ($page instanceof HtmlPage) { // In case renderPage() returns NULL due to an error cast it to a string // so as to not cause issues with Response. This also allows renderPage // to return an object implementing __toString(), but that is not // recommended. $response = new Response((string) $this->pageRenderer->render($page), $page->getStatusCode()); if ($tags = $page->getCacheTags()) { $response->headers->set('X-Drupal-Cache-Tags', static::convertCacheTagsToHeader($tags)); } if ($keys = $page->getCacheKeys()) { $response->headers->set('cache_keys', serialize($keys)); } if ($bin = $page->getCacheBin()) { $response->headers->set('cache_bin', $bin); } if ($max_age = $page->getCacheMaxAge()) { $response->headers->set('cache_max_age', $max_age); } // Set the generator in the HTTP header. list($version) = explode('.', \Drupal::VERSION, 2); $response->headers->set('X-Generator', 'Drupal ' . $version . ' (http://drupal.org)'); $event->setResponse($response); } }
/** * @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; }