3 namespace Drupal\Core\Routing;
5 use Symfony\Cmf\Component\Routing\RouteObjectInterface;
6 use Symfony\Component\HttpFoundation\ParameterBag;
7 use Symfony\Component\HttpFoundation\Request;
8 use Symfony\Component\Routing\Route;
11 * Default object representing the results of routing.
13 class RouteMatch implements RouteMatchInterface {
25 * @var \Symfony\Component\Routing\Route
30 * A key|value store of parameters.
32 * @var \Symfony\Component\HttpFoundation\ParameterBag
34 protected $parameters;
37 * A key|value store of raw parameters.
39 * @var \Symfony\Component\HttpFoundation\ParameterBag
41 protected $rawParameters;
44 * Constructs a RouteMatch object.
46 * @param string $route_name
47 * The name of the route.
48 * @param \Symfony\Component\Routing\Route $route
50 * @param array $parameters
51 * The parameters array.
52 * @param array $raw_parameters
53 * The raw $parameters array.
55 public function __construct($route_name, Route $route, array $parameters = [], array $raw_parameters = []) {
56 $this->routeName = $route_name;
57 $this->route = $route;
59 // Pre-filter parameters.
60 $route_params = $this->getParameterNames();
61 $parameters = array_intersect_key($parameters, $route_params);
62 $raw_parameters = array_intersect_key($raw_parameters, $route_params);
63 $this->parameters = new ParameterBag($parameters);
64 $this->rawParameters = new ParameterBag($raw_parameters);
68 * Creates a RouteMatch from a request.
70 * @param Request $request
73 * @return \Drupal\Core\Routing\RouteMatchInterface
74 * A new RouteMatch object if there's a matched route for the request.
75 * A new NullRouteMatch object otherwise (e.g., on a 404 page or when
76 * invoked prior to routing).
78 public static function createFromRequest(Request $request) {
79 if ($request->attributes->get(RouteObjectInterface::ROUTE_OBJECT)) {
81 if ($raw = $request->attributes->get('_raw_variables')) {
82 $raw_variables = $raw->all();
85 $request->attributes->get(RouteObjectInterface::ROUTE_NAME),
86 $request->attributes->get(RouteObjectInterface::ROUTE_OBJECT),
87 $request->attributes->all(),
91 return new NullRouteMatch();
98 public function getRouteName() {
99 return $this->routeName;
105 public function getRouteObject() {
112 public function getParameter($parameter_name) {
113 return $this->parameters->get($parameter_name);
119 public function getParameters() {
120 return $this->parameters;
126 public function getRawParameter($parameter_name) {
127 return $this->rawParameters->get($parameter_name);
133 public function getRawParameters() {
134 return $this->rawParameters;
138 * Returns the names of all parameters for the currently matched route.
141 * Route parameter names as both the keys and values.
143 protected function getParameterNames() {
145 if ($route = $this->getRouteObject()) {
146 // Variables defined in path and host patterns are route parameters.
147 $variables = $route->compile()->getVariables();
148 $names = array_combine($variables, $variables);
149 // Route defaults that do not start with a leading "_" are also
150 // parameters, even if they are not included in path or host patterns.
151 foreach ($route->getDefaults() as $name => $value) {
152 if (!isset($names[$name]) && substr($name, 0, 1) !== '_') {
153 $names[$name] = $name;