class RedirectDestination

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

Provides helpers for redirect destinations.

Hierarchy

  • class \Drupal\Core\Routing\RedirectDestination implements \Drupal\Core\Routing\RedirectDestinationInterface

Expanded class hierarchy of RedirectDestination

1 file declares its use of RedirectDestination
RedirectDestinationTest.php in core/tests/Drupal/Tests/Core/Routing/RedirectDestinationTest.php
1 string reference to 'RedirectDestination'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses RedirectDestination
redirect.destination in core/core.services.yml
Drupal\Core\Routing\RedirectDestination

File

core/lib/Drupal/Core/Routing/RedirectDestination.php, line 11

Namespace

Drupal\Core\Routing
View source
class RedirectDestination implements RedirectDestinationInterface {
    
    /**
     * The request stack.
     *
     * @var \Symfony\Component\HttpFoundation\RequestStack
     */
    protected $requestStack;
    
    /**
     * The URL generator.
     *
     * @var \Drupal\Core\Routing\UrlGeneratorInterface
     */
    protected $urlGenerator;
    
    /**
     * The destination used by the current request.
     *
     * @var string
     */
    protected $destination;
    
    /**
     * Constructs a new RedirectDestination instance.
     *
     * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
     *   The request stack.
     * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
     *   The URL generator.
     */
    public function __construct(RequestStack $request_stack, UrlGeneratorInterface $url_generator) {
        $this->requestStack = $request_stack;
        $this->urlGenerator = $url_generator;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getAsArray() {
        return [
            'destination' => $this->get(),
        ];
    }
    
    /**
     * {@inheritdoc}
     */
    public function get() {
        if (!isset($this->destination)) {
            $query = $this->requestStack
                ->getCurrentRequest()->query;
            if ($query->has('destination')) {
                $this->destination = $query->get('destination');
                if (UrlHelper::isExternal($this->destination)) {
                    // See https://www.drupal.org/node/2454955 for external redirects.
                    $this->destination = '/';
                }
            }
            else {
                $this->destination = $this->urlGenerator
                    ->generateFromRoute('<current>', [], [
                    'query' => UrlHelper::filterQueryParameters($query->all()),
                ]);
            }
        }
        return $this->destination;
    }
    
    /**
     * {@inheritdoc}
     */
    public function set($new_destination) {
        $this->destination = $new_destination;
        return $this;
    }

}

Members

Title Sort descending Modifiers Object type Summary
RedirectDestination::$destination protected property The destination used by the current request.
RedirectDestination::$requestStack protected property The request stack.
RedirectDestination::$urlGenerator protected property The URL generator.
RedirectDestination::get public function
RedirectDestination::getAsArray public function
RedirectDestination::set public function
RedirectDestination::__construct public function Constructs a new RedirectDestination instance.

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