function RoutePreloader::onRequest

Same name in other branches
  1. 9 core/lib/Drupal/Core/Routing/RoutePreloader.php \Drupal\Core\Routing\RoutePreloader::onRequest()
  2. 10 core/lib/Drupal/Core/Routing/RoutePreloader.php \Drupal\Core\Routing\RoutePreloader::onRequest()
  3. 11.x core/lib/Drupal/Core/Routing/RoutePreloader.php \Drupal\Core\Routing\RoutePreloader::onRequest()

Loads all non-admin routes right before the actual page is rendered.

Parameters

\Symfony\Component\HttpKernel\Event\KernelEvent $event: The event to process.

File

core/lib/Drupal/Core/Routing/RoutePreloader.php, line 72

Class

RoutePreloader
Defines a class which preloads non-admin routes.

Namespace

Drupal\Core\Routing

Code

public function onRequest(KernelEvent $event) {
    // Only preload on normal HTML pages, as they will display menu links.
    if ($this->routeProvider instanceof PreloadableRouteProviderInterface && $event->getRequest()
        ->getRequestFormat() == 'html') {
        // Ensure that the state query is cached to skip the database query, if
        // possible.
        $key = 'routing.non_admin_routes';
        if ($cache = $this->cache
            ->get($key)) {
            $routes = $cache->data;
        }
        else {
            $routes = $this->state
                ->get($key, []);
            $this->cache
                ->set($key, $routes, Cache::PERMANENT, [
                'routes',
            ]);
        }
        if ($routes) {
            // Preload all the non-admin routes at once.
            $this->routeProvider
                ->preLoadRoutes($routes);
        }
    }
}

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