TrustedRedirectResponse.php

Same filename in other branches
  1. 9 core/lib/Drupal/Core/Routing/TrustedRedirectResponse.php
  2. 8.9.x core/lib/Drupal/Core/Routing/TrustedRedirectResponse.php
  3. 10 core/lib/Drupal/Core/Routing/TrustedRedirectResponse.php

Namespace

Drupal\Core\Routing

File

core/lib/Drupal/Core/Routing/TrustedRedirectResponse.php

View source
<?php

namespace Drupal\Core\Routing;


/**
 * Provides a redirect response which contains trusted URLs.
 *
 * Use this class in case you know that you want to redirect to an external URL.
 */
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);
    }

}

Classes

Title Deprecated Summary
TrustedRedirectResponse Provides a redirect response which contains trusted URLs.

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