Same name and namespace in other branches
  1. 8.9.x core/lib/Drupal/Core/Routing/TrustedRedirectResponse.php \Drupal\Core\Routing\TrustedRedirectResponse
  2. 9 core/lib/Drupal/Core/Routing/TrustedRedirectResponse.php \Drupal\Core\Routing\TrustedRedirectResponse

Provides a redirect response which contains trusted URLs.

Use this class in case you know that you want to redirect to an external URL.

Hierarchy

Expanded class hierarchy of TrustedRedirectResponse

3 files declare their use of TrustedRedirectResponse
Cookie.php in core/modules/user/src/Authentication/Provider/Cookie.php
TrustedRedirectResponseTest.php in core/tests/Drupal/Tests/Core/Routing/TrustedRedirectResponseTest.php
UserAuthTest.php in core/modules/user/tests/src/Unit/UserAuthTest.php

File

core/lib/Drupal/Core/Routing/TrustedRedirectResponse.php, line 10

Namespace

Drupal\Core\Routing
View source
class TrustedRedirectResponse extends CacheableSecuredRedirectResponse {
  use LocalAwareRedirectResponseTrait;

  /**
   * A list of trusted URLs, which are safe to redirect to.
   *
   * @var string[]
   */
  protected $trustedUrls = [];

  /**
   * {@inheritdoc}
   */
  public function __construct($url, $status = 302, $headers = []) {
    $this->trustedUrls[$url] = TRUE;
    parent::__construct($url, $status, $headers);
  }

  /**
   * Sets the target URL to a trusted URL.
   *
   * @param string $url
   *   A trusted URL.
   *
   * @return $this
   */
  public function setTrustedTargetUrl($url) {
    $this->trustedUrls[$url] = TRUE;
    return $this
      ->setTargetUrl($url);
  }

  /**
   * {@inheritdoc}
   */
  protected function isSafe($url) {
    return !empty($this->trustedUrls[$url]) || $this
      ->isLocal($url);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheableResponseTrait::$cacheabilityMetadata protected property The cacheability metadata.
CacheableResponseTrait::addCacheableDependency public function
CacheableResponseTrait::getCacheableMetadata public function
CacheableSecuredRedirectResponse::fromResponse protected function
LocalAwareRedirectResponseTrait::$requestContext protected property The request context.
LocalAwareRedirectResponseTrait::getRequestContext protected function Returns the request context.
LocalAwareRedirectResponseTrait::isLocal protected function Determines whether a path is local.
LocalAwareRedirectResponseTrait::setRequestContext public function Sets the request context.
TrustedRedirectResponse::$trustedUrls protected property A list of trusted URLs, which are safe to redirect to.
TrustedRedirectResponse::isSafe protected function
TrustedRedirectResponse::setTrustedTargetUrl public function Sets the target URL to a trusted URL.
TrustedRedirectResponse::__construct public function