function RouteProvider::preLoadRoutes

Same name in this branch
  1. main core/tests/Drupal/KernelTests/RouteProvider.php \Drupal\KernelTests\RouteProvider::preLoadRoutes()
Same name and namespace in other branches
  1. 11.x core/tests/Drupal/KernelTests/RouteProvider.php \Drupal\KernelTests\RouteProvider::preLoadRoutes()
  2. 11.x core/lib/Drupal/Core/Routing/RouteProvider.php \Drupal\Core\Routing\RouteProvider::preLoadRoutes()
  3. 10 core/tests/Drupal/KernelTests/RouteProvider.php \Drupal\KernelTests\RouteProvider::preLoadRoutes()
  4. 10 core/lib/Drupal/Core/Routing/RouteProvider.php \Drupal\Core\Routing\RouteProvider::preLoadRoutes()
  5. 9 core/tests/Drupal/KernelTests/RouteProvider.php \Drupal\KernelTests\RouteProvider::preLoadRoutes()
  6. 9 core/lib/Drupal/Core/Routing/RouteProvider.php \Drupal\Core\Routing\RouteProvider::preLoadRoutes()
  7. 8.9.x core/tests/Drupal/KernelTests/RouteProvider.php \Drupal\KernelTests\RouteProvider::preLoadRoutes()
  8. 8.9.x core/lib/Drupal/Core/Routing/RouteProvider.php \Drupal\Core\Routing\RouteProvider::preLoadRoutes()

Pre-load routes by their names using the provided list of names.

This method exists in order to allow performance optimizations. It allows pre-loading serialized routes that may latter be retrieved using ::getRoutesByName()

Parameters

string[] $names: Array of route names to load.

Overrides PreloadableRouteProviderInterface::preLoadRoutes

1 call to RouteProvider::preLoadRoutes()
RouteProvider::getRoutesByNames in core/lib/Drupal/Core/Routing/RouteProvider.php
Find many routes by their names using the provided list of names.

File

core/lib/Drupal/Core/Routing/RouteProvider.php, line 241

Class

RouteProvider
A Route Provider front-end for all Drupal-stored routes.

Namespace

Drupal\Core\Routing

Code

public function preLoadRoutes($names) {
  if (empty($names)) {
    throw new \InvalidArgumentException('You must specify the route names to load');
  }
  $routes_to_load = array_diff($names, array_keys($this->routes));
  if ($routes_to_load) {
    $cached = $this->cache
      ->getMultiple($routes_to_load);
    foreach ($cached as $cid => $item) {
      $this->routes[$cid] = $item->data;
    }
    // \Drupal\Core\Cache\CacheBackendInterface::getMultiple() removed the
    // routes that were found in the cache from the variable. Load the
    // remaining ones, if any, from the database.
    if ($routes_to_load) {
      try {
        $result = $this->connection
          ->query('SELECT [name], [route] FROM {' . $this->connection
          ->escapeTable($this->tableName) . '} WHERE [name] IN ( :names[] )', [
          ':names[]' => $routes_to_load,
        ]);
        $loaded_routes = array_map('unserialize', $result->fetchAllKeyed());
        $this->routes += $loaded_routes;
        $items = [];
        foreach ($loaded_routes as $route_name => $data) {
          $items[$route_name] = [
            'data' => $data,
            'expire' => CacheBackendInterface::CACHE_PERMANENT,
            'tags' => [
              'routes',
            ],
          ];
        }
        if ($items) {
          $this->cache
            ->setMultiple($items);
        }
      } catch (\Exception) {
      }
    }
  }
}

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