Change record status: 
Project: 
Introduced in branch: 
8.0.x
Introduced in version: 
8.0-BETA5
Description: 

In order to not load every route enhancer / filter on every request, a new interface for route filters / enhancers got introduced which allows them to specify when they run.

These new interfaces are Drupal\Core\Routing\RouteEnhancerInterface and \Drupal\Core\RouteFilterInterface, and they extend \Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface and \Symfony\Cmf\Component\Routing\NestedMatcher\RouteFilterInterface, respectively.

The new ::applies() method is executed when the route is dumped into the database, so you can't rely on any request-specific method in there. It should return TRUE, if the route filter / enhancer should be executed for the given route.

(The consequence is that only the route enhancers that are necessary for a route will be initialized, and hence the services that the route enhancer depends upon will also only be initialized when necessary. The end result is that fewer services need to be initialized on each page request.)

One example implementation is the EntityRouteEnhancer, see

  /**
   * {@inheritdoc}
   */
  public function applies(Route $route) {
    return !$route->hasDefault('_controller') &&
      ($route->hasDefault('_entity_form')
        || $route->hasDefault('_entity_list')
        || $route->hasDefault('_entity_view')
      );
  }
Impacts: 
Module developers