You are here

TrustedRedirectResponse.php in Drupal 9

Same filename and directory in other branches
  1. 8 core/lib/Drupal/Core/Routing/TrustedRedirectResponse.php

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

Namesort descending Description
TrustedRedirectResponse Provides a redirect response which contains trusted URLs.