You are here

class RouteMatch in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Routing/RouteMatch.php \Drupal\Core\Routing\RouteMatch

Default object representing the results of routing.

Hierarchy

Expanded class hierarchy of RouteMatch

17 files declare their use of RouteMatch
AccessDeniedSubscriber.php in core/modules/user/src/EventSubscriber/AccessDeniedSubscriber.php
Contains \Drupal\user\EventSubscriber\AccessDeniedSubscriber.
AccessManager.php in core/lib/Drupal/Core/Access/AccessManager.php
Contains \Drupal\Core\Access\AccessManager.
AccessManagerTest.php in core/tests/Drupal/Tests/Core/Access/AccessManagerTest.php
Contains \Drupal\Tests\Core\Access\AccessManagerTest.
AuthenticationManager.php in core/lib/Drupal/Core/Authentication/AuthenticationManager.php
Contains \Drupal\Core\Authentication\AuthenticationManager.
ConfigNamesMapperTest.php in core/modules/config_translation/tests/src/Unit/ConfigNamesMapperTest.php
Contains \Drupal\Tests\config_translation\Unit\ConfigNamesMapperTest.

... See full list

File

core/lib/Drupal/Core/Routing/RouteMatch.php, line 18
Contains \Drupal\Core\Routing\RouteMatch.

Namespace

Drupal\Core\Routing
View source
class RouteMatch implements RouteMatchInterface {

  /**
   * The route name.
   *
   * @var string
   */
  protected $routeName;

  /**
   * The route.
   *
   * @var \Symfony\Component\Routing\Route
   */
  protected $route;

  /**
   * A key|value store of parameters.
   *
   * @var \Symfony\Component\HttpFoundation\ParameterBag
   */
  protected $parameters;

  /**
   * A key|value store of raw parameters.
   *
   * @var \Symfony\Component\HttpFoundation\ParameterBag
   */
  protected $rawParameters;

  /**
   * Constructs a RouteMatch object.
   *
   * @param string $route_name
   *  The name of the route.
   * @param \Symfony\Component\Routing\Route $route
   *   The route.
   * @param array $parameters
   *   The parameters array.
   * @param array $raw_parameters
   *   The raw $parameters array.
   */
  public function __construct($route_name, Route $route, array $parameters = array(), array $raw_parameters = array()) {
    $this->routeName = $route_name;
    $this->route = $route;

    // Pre-filter parameters.
    $route_params = $this
      ->getParameterNames();
    $parameters = array_intersect_key($parameters, $route_params);
    $raw_parameters = array_intersect_key($raw_parameters, $route_params);
    $this->parameters = new ParameterBag($parameters);
    $this->rawParameters = new ParameterBag($raw_parameters);
  }

  /**
   * Creates a RouteMatch from a request.
   *
   * @param Request $request
   *   A request object.
   *
   * @return \Drupal\Core\Routing\RouteMatchInterface
   *   A new RouteMatch object if there's a matched route for the request.
   *   A new NullRouteMatch object otherwise (e.g., on a 404 page or when
   *   invoked prior to routing).
   */
  public static function createFromRequest(Request $request) {
    if ($request->attributes
      ->get(RouteObjectInterface::ROUTE_OBJECT)) {
      $raw_variables = array();
      if ($raw = $request->attributes
        ->get('_raw_variables')) {
        $raw_variables = $raw
          ->all();
      }
      return new static($request->attributes
        ->get(RouteObjectInterface::ROUTE_NAME), $request->attributes
        ->get(RouteObjectInterface::ROUTE_OBJECT), $request->attributes
        ->all(), $raw_variables);
    }
    else {
      return new NullRouteMatch();
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getRouteName() {
    return $this->routeName;
  }

  /**
   * {@inheritdoc}
   */
  public function getRouteObject() {
    return $this->route;
  }

  /**
   * {@inheritdoc}
   */
  public function getParameter($parameter_name) {
    return $this->parameters
      ->get($parameter_name);
  }

  /**
   * {@inheritdoc}
   */
  public function getParameters() {
    return $this->parameters;
  }

  /**
   * {@inheritdoc}
   */
  public function getRawParameter($parameter_name) {
    return $this->rawParameters
      ->get($parameter_name);
  }

  /**
   * {@inheritdoc}
   */
  public function getRawParameters() {
    return $this->rawParameters;
  }

  /**
   * Returns the names of all parameters for the currently matched route.
   *
   * @return array
   *   Route parameter names as both the keys and values.
   */
  protected function getParameterNames() {
    $names = array();
    if ($route = $this
      ->getRouteObject()) {

      // Variables defined in path and host patterns are route parameters.
      $variables = $route
        ->compile()
        ->getVariables();
      $names = array_combine($variables, $variables);

      // Route defaults that do not start with a leading "_" are also
      // parameters, even if they are not included in path or host patterns.
      foreach ($route
        ->getDefaults() as $name => $value) {
        if (!isset($names[$name]) && substr($name, 0, 1) !== '_') {
          $names[$name] = $name;
        }
      }
    }
    return $names;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RouteMatch::$parameters protected property A key|value store of parameters.
RouteMatch::$rawParameters protected property A key|value store of raw parameters.
RouteMatch::$route protected property The route.
RouteMatch::$routeName protected property The route name.
RouteMatch::createFromRequest public static function Creates a RouteMatch from a request.
RouteMatch::getParameter public function Returns the processed value of a named route parameter. Overrides RouteMatchInterface::getParameter
RouteMatch::getParameterNames protected function Returns the names of all parameters for the currently matched route.
RouteMatch::getParameters public function Returns the bag of all processed route parameters. Overrides RouteMatchInterface::getParameters
RouteMatch::getRawParameter public function Returns the raw value of a named route parameter. Overrides RouteMatchInterface::getRawParameter
RouteMatch::getRawParameters public function Returns the bag of all raw route parameters. Overrides RouteMatchInterface::getRawParameters
RouteMatch::getRouteName public function Returns the route name. Overrides RouteMatchInterface::getRouteName
RouteMatch::getRouteObject public function Returns the route object. Overrides RouteMatchInterface::getRouteObject
RouteMatch::__construct public function Constructs a RouteMatch object.