SpecialAttributesRouteSubscriber.php

Same filename and directory in other branches
  1. 9 core/lib/Drupal/Core/EventSubscriber/SpecialAttributesRouteSubscriber.php
  2. 8.9.x core/lib/Drupal/Core/EventSubscriber/SpecialAttributesRouteSubscriber.php
  3. 10 core/lib/Drupal/Core/EventSubscriber/SpecialAttributesRouteSubscriber.php

Namespace

Drupal\Core\EventSubscriber

File

core/lib/Drupal/Core/EventSubscriber/SpecialAttributesRouteSubscriber.php

View source
<?php

namespace Drupal\Core\EventSubscriber;

use Drupal\Core\Routing\RouteBuildEvent;
use Drupal\Core\Routing\RouteSubscriberBase;
use Drupal\Core\Routing\RouteObjectInterface;
use Symfony\Component\Routing\RouteCollection;

/**
 * Provides a route subscriber which checks for invalid pattern variables.
 */
class SpecialAttributesRouteSubscriber extends RouteSubscriberBase {
  
  /**
   * {@inheritdoc}
   */
  protected function alterRoutes(RouteCollection $collection) {
    $special_variables = [
      'system_path',
      '_legacy',
      '_raw_variables',
      RouteObjectInterface::ROUTE_OBJECT,
      RouteObjectInterface::ROUTE_NAME,
      '_content',
      '_controller',
      '_form',
    ];
    foreach ($collection->all() as $name => $route) {
      if ($not_allowed_variables = array_intersect($route->compile()
        ->getVariables(), $special_variables)) {
        $reserved = implode(', ', $not_allowed_variables);
        throw new \InvalidArgumentException(sprintf('Route %s uses reserved variable names: %s', $name, $reserved));
      }
    }
  }
  
  /**
   * Delegates the route altering to self::alterRoutes().
   *
   * @param \Drupal\Core\Routing\RouteBuildEvent $event
   *   The route build event.
   */
  public function onAlterRoutes(RouteBuildEvent $event) {
    $collection = $event->getRouteCollection();
    $this->alterRoutes($collection);
  }

}

Classes

Title Deprecated Summary
SpecialAttributesRouteSubscriber Provides a route subscriber which checks for invalid pattern variables.

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