class CurrentRouteMatch

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Routing/CurrentRouteMatch.php \Drupal\Core\Routing\CurrentRouteMatch
  2. 8.9.x core/lib/Drupal/Core/Routing/CurrentRouteMatch.php \Drupal\Core\Routing\CurrentRouteMatch
  3. 10 core/lib/Drupal/Core/Routing/CurrentRouteMatch.php \Drupal\Core\Routing\CurrentRouteMatch

Default object for current_route_match service.

Hierarchy

Expanded class hierarchy of CurrentRouteMatch

3 files declare their use of CurrentRouteMatch
ContentPreprocessTest.php in core/modules/content_moderation/tests/src/Unit/ContentPreprocessTest.php
CurrentRouteMatchTest.php in core/tests/Drupal/Tests/Core/Routing/CurrentRouteMatchTest.php
MenuActiveTrailTest.php in core/tests/Drupal/Tests/Core/Menu/MenuActiveTrailTest.php

File

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

Namespace

Drupal\Core\Routing
View source
class CurrentRouteMatch implements ResettableStackedRouteMatchInterface {
    
    /**
     * The related request stack.
     *
     * @var \Symfony\Component\HttpFoundation\RequestStack
     */
    protected $requestStack;
    
    /**
     * Internal cache of RouteMatch objects.
     *
     * @var \SplObjectStorage
     */
    protected $routeMatches;
    
    /**
     * Constructs a CurrentRouteMatch object.
     *
     * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
     *   The request stack.
     */
    public function __construct(RequestStack $request_stack) {
        $this->requestStack = $request_stack;
        $this->routeMatches = new \SplObjectStorage();
    }
    
    /**
     * {@inheritdoc}
     */
    public function getRouteName() {
        return $this->getCurrentRouteMatch()
            ->getRouteName();
    }
    
    /**
     * {@inheritdoc}
     */
    public function getRouteObject() {
        return $this->getCurrentRouteMatch()
            ->getRouteObject();
    }
    
    /**
     * {@inheritdoc}
     */
    public function getParameter($parameter_name) {
        return $this->getCurrentRouteMatch()
            ->getParameter($parameter_name);
    }
    
    /**
     * {@inheritdoc}
     */
    public function getParameters() {
        return $this->getCurrentRouteMatch()
            ->getParameters();
    }
    
    /**
     * {@inheritdoc}
     */
    public function getRawParameter($parameter_name) {
        return $this->getCurrentRouteMatch()
            ->getRawParameter($parameter_name);
    }
    
    /**
     * {@inheritdoc}
     */
    public function getRawParameters() {
        return $this->getCurrentRouteMatch()
            ->getRawParameters();
    }
    
    /**
     * Returns the route match for the current request.
     *
     * @return \Drupal\Core\Routing\RouteMatchInterface
     *   The current route match object.
     */
    public function getCurrentRouteMatch() {
        return $this->getRouteMatch($this->requestStack
            ->getCurrentRequest());
    }
    
    /**
     * Returns the route match for a passed in request.
     *
     * @param \Symfony\Component\HttpFoundation\Request $request
     *   A request object.
     *
     * @return \Drupal\Core\Routing\RouteMatchInterface
     *   A route match object created from the request.
     */
    protected function getRouteMatch(Request $request) {
        if (isset($this->routeMatches[$request])) {
            $route_match = $this->routeMatches[$request];
        }
        else {
            $route_match = RouteMatch::createFromRequest($request);
            // Since getRouteMatch() might be invoked both before and after routing
            // is completed, only statically cache the route match after there's a
            // matched route.
            if ($route_match->getRouteObject()) {
                $this->routeMatches[$request] = $route_match;
            }
        }
        return $route_match;
    }
    
    /**
     * {@inheritdoc}
     */
    public function resetRouteMatch() {
        $this->routeMatches = new \SplObjectStorage();
    }
    
    /**
     * {@inheritdoc}
     */
    public function getMasterRouteMatch() {
        return $this->getRouteMatch($this->requestStack
            ->getMainRequest());
    }
    
    /**
     * {@inheritdoc}
     */
    public function getParentRouteMatch() {
        return $this->getRouteMatch($this->requestStack
            ->getParentRequest());
    }
    
    /**
     * {@inheritdoc}
     */
    public function getRouteMatchFromRequest(Request $request) {
        return $this->getRouteMatch($request);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
CurrentRouteMatch::$requestStack protected property The related request stack.
CurrentRouteMatch::$routeMatches protected property Internal cache of RouteMatch objects.
CurrentRouteMatch::getCurrentRouteMatch public function Returns the route match for the current request. Overrides StackedRouteMatchInterface::getCurrentRouteMatch
CurrentRouteMatch::getMasterRouteMatch public function Gets the master route match.. Overrides StackedRouteMatchInterface::getMasterRouteMatch
CurrentRouteMatch::getParameter public function Returns the processed value of a named route parameter. Overrides RouteMatchInterface::getParameter
CurrentRouteMatch::getParameters public function Returns the bag of all processed route parameters. Overrides RouteMatchInterface::getParameters
CurrentRouteMatch::getParentRouteMatch public function Returns the parent route match of the current. Overrides StackedRouteMatchInterface::getParentRouteMatch
CurrentRouteMatch::getRawParameter public function Returns the raw value of a named route parameter. Overrides RouteMatchInterface::getRawParameter
CurrentRouteMatch::getRawParameters public function Returns the bag of all raw route parameters. Overrides RouteMatchInterface::getRawParameters
CurrentRouteMatch::getRouteMatch protected function Returns the route match for a passed in request.
CurrentRouteMatch::getRouteMatchFromRequest public function Returns a route match from a given request, if possible. Overrides StackedRouteMatchInterface::getRouteMatchFromRequest
CurrentRouteMatch::getRouteName public function Returns the route name. Overrides RouteMatchInterface::getRouteName
CurrentRouteMatch::getRouteObject public function Returns the route object. Overrides RouteMatchInterface::getRouteObject
CurrentRouteMatch::resetRouteMatch public function Resets the route match static cache. Overrides ResettableStackedRouteMatchInterface::resetRouteMatch
CurrentRouteMatch::__construct public function Constructs a CurrentRouteMatch object.

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