class TrustedRedirectResponse

Same name in other branches
  1. 9 core/lib/Drupal/Core/Routing/TrustedRedirectResponse.php \Drupal\Core\Routing\TrustedRedirectResponse
  2. 10 core/lib/Drupal/Core/Routing/TrustedRedirectResponse.php \Drupal\Core\Routing\TrustedRedirectResponse
  3. 11.x 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

2 files declare their use of TrustedRedirectResponse
RedirectResponseSubscriberTest.php in core/tests/Drupal/Tests/Core/EventSubscriber/RedirectResponseSubscriberTest.php
TrustedRedirectResponseTest.php in core/tests/Drupal/Tests/Core/Routing/TrustedRedirectResponseTest.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

Title Sort descending Modifiers Object type Summary Overriden Title
CacheableResponseTrait::$cacheabilityMetadata protected property The cacheability metadata.
CacheableResponseTrait::addCacheableDependency public function
CacheableResponseTrait::getCacheableMetadata public function
CacheableSecuredRedirectResponse::fromResponse protected function Copies over the values from the given response. Overrides SecuredRedirectResponse::fromResponse
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.
SecuredRedirectResponse::createFromRedirectResponse public static function Copies an existing redirect response into a safe one.
SecuredRedirectResponse::setTargetUrl public function
TrustedRedirectResponse::$trustedUrls protected property A list of trusted URLs, which are safe to redirect to.
TrustedRedirectResponse::isSafe protected function Returns whether the URL is considered as safe to redirect to. Overrides SecuredRedirectResponse::isSafe
TrustedRedirectResponse::setTrustedTargetUrl public function Sets the target URL to a trusted URL.
TrustedRedirectResponse::__construct public function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.