trait LocalAwareRedirectResponseTrait

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Routing/LocalAwareRedirectResponseTrait.php \Drupal\Core\Routing\LocalAwareRedirectResponseTrait
  2. 10 core/lib/Drupal/Core/Routing/LocalAwareRedirectResponseTrait.php \Drupal\Core\Routing\LocalAwareRedirectResponseTrait
  3. 11.x core/lib/Drupal/Core/Routing/LocalAwareRedirectResponseTrait.php \Drupal\Core\Routing\LocalAwareRedirectResponseTrait

Provides a trait which ensures that a URL is safe to redirect to.

Hierarchy

File

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

Namespace

Drupal\Core\Routing
View source
trait LocalAwareRedirectResponseTrait {
    
    /**
     * The request context.
     *
     * @var \Drupal\Core\Routing\RequestContext
     */
    protected $requestContext;
    
    /**
     * Determines whether a path is local.
     *
     * @param string $url
     *   The internal path or external URL being linked to, such as "node/34" or
     *   "http://example.com/foo".
     *
     * @return bool
     *   TRUE or FALSE, where TRUE indicates a local path.
     */
    protected function isLocal($url) {
        return !UrlHelper::isExternal($url) || UrlHelper::externalIsLocal($url, $this->getRequestContext()
            ->getCompleteBaseUrl());
    }
    
    /**
     * Returns the request context.
     *
     * @return \Drupal\Core\Routing\RequestContext
     *   The request context.
     */
    protected function getRequestContext() {
        if (!isset($this->requestContext)) {
            $this->requestContext = \Drupal::service('router.request_context');
        }
        return $this->requestContext;
    }
    
    /**
     * Sets the request context.
     *
     * @param \Drupal\Core\Routing\RequestContext $request_context
     *   The request context.
     *
     * @return $this
     */
    public function setRequestContext(RequestContext $request_context) {
        $this->requestContext = $request_context;
        return $this;
    }

}

Members

Title Sort descending Modifiers Object type Summary
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.

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