allow route enhancers to determine whether they apply at compile time rather than during requests

Created on 14 January 2023, almost 2 years ago
Updated 7 March 2023, almost 2 years ago

Problem/Motivation

Currently, during a request, Router::matchRequest() calls applyRouteEnhancers(), and every route enhancer is run on the route.

Core has 7 classes that implement EnhancerInterface, so that's not a big performance cost, but still, it means that on *every* request, EntityRouteEnhancer, say, has to check if the it applies to the route. And it's a performance cost that rises if contrib or custom code adds route enhancers.

But, EntityRouteEnhancer's check to see if it should act has nothing to do with the incoming request, and only looks at the route:

  protected function applies(Route $route) {
    return !$route->hasDefault('_controller') &&
      ($route->hasDefault('_entity_form')
        || $route->hasDefault('_entity_list')
        || $route->hasDefault('_entity_view')
      );
  }

That's also the case with all of core's route enhancers.

Proposed resolution

Add a method EnhancerInterface::appliesToRoute() to determine at compile time whether the enhancer applies to a route. The RouteBuilder then stores this value in the compiled route's options.

Then on request, Router::applyRouteEnhancers() runs only the enhancers that apply.

If there are any enhancers in contrib or custom code that determine whether they apply based on request values, they can simply return TRUE for appliesToRoute(), and that way they are always called for routes.

Remaining tasks

- add the method
- update core enhancers
- write tests

User interface changes

None.

API changes

New method on EnhancerInterface.

Data model changes

Release notes snippet

Feature request
Status

Active

Version

10.1

Component
Routing 

Last updated 3 days ago

Created by

🇬🇧United Kingdom joachim

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024