You are here

protected function RedirectRequestSubscriber::setResponse in Redirect 8

Prior to set the response it check if we can redirect.

Parameters

\Symfony\Component\HttpKernel\Event\GetResponseEvent $event: The event object.

\Drupal\Core\Url $url: The Url where we want to redirect.

File

src/EventSubscriber/RedirectRequestSubscriber.php, line 180

Class

RedirectRequestSubscriber
Redirect subscriber for controller requests.

Namespace

Drupal\redirect\EventSubscriber

Code

protected function setResponse(GetResponseEvent $event, Url $url) {
  $request = $event
    ->getRequest();
  $this->context
    ->fromRequest($request);
  parse_str($request
    ->getQueryString(), $query);
  $url
    ->setOption('query', $query);
  $url
    ->setAbsolute(TRUE);

  // We can only check access for routed URLs.
  if (!$url
    ->isRouted() || $this->checker
    ->canRedirect($request, $url
    ->getRouteName())) {

    // Add the 'rendered' cache tag, so that we can invalidate all responses
    // when settings are changed.
    $response = new TrustedRedirectResponse($url
      ->toString(), 301);
    $response
      ->addCacheableDependency(CacheableMetadata::createFromRenderArray([])
      ->addCacheTags([
      'rendered',
    ]));
    $event
      ->setResponse($response);
  }
}