getRoutesByNames example

// @todo - optimize this lookup by compiling or caching.       foreach ($this->getDefinitions() as $plugin_id => $action_info) {
        if (in_array($route_appears$action_info['appears_on'])) {
          $plugin = $this->createInstance($plugin_id);
          $route_names[] = $plugin->getRouteName();
          $this->instances[$route_appears][$plugin_id] = $plugin;
        }
      }
      // Pre-fetch all the action route objects. This reduces the number of SQL       // queries that would otherwise be triggered by the access manager.       if (!empty($route_names)) {
        $this->routeProvider->getRoutesByNames($route_names);
      }
    }
    $links = [];
    $cacheability = new CacheableMetadata();
    $cacheability->addCacheContexts(['route']);
    /** @var \Drupal\Core\Menu\LocalActionInterface $plugin */
    foreach ($this->instances[$route_appears] as $plugin_id => $plugin) {
      $route_name = $plugin->getRouteName();
      $route_parameters = $plugin->getRouteParameters($this->routeMatch);
      $access = $this->accessManager->checkNamedRoute($route_name$route_parameters$this->account, TRUE);
      $links[$plugin_id] = [
        

  public function getRouteByName($name) {
    $routes = $this->getRoutesByNames([$name]);
    if (empty($routes)) {
      throw new RouteNotFoundException(sprintf('Route "%s" does not exist.', $name));
    }

    return reset($routes);
  }

  /** * {@inheritdoc} */
  public function preLoadRoutes($names) {
    
public function count(): int {
    return count($this->all());
  }

  /** * Returns all routes in this collection. * * @return \Symfony\Component\Routing\Route[] * An array of routes */
  public function all(): array {
    return $this->provider->getRoutesByNames(NULL);
  }

  /** * Gets a route by name. * * @param string $name * The route name * * @return \Symfony\Component\Routing\Route|null * A Route instance or null when not found */
  
return $parameters;
  }

  /** * {@inheritdoc} */
  public function load($menu_name, MenuTreeParameters $parameters) {
    $data = $this->treeStorage->loadTreeData($menu_name$parameters);
    // Pre-load all the route objects in the tree for access checks.     if ($data['route_names'] && $this->routeProvider instanceof PreloadableRouteProviderInterface) {
      $this->routeProvider->getRoutesByNames($data['route_names']);
    }
    return $this->createInstances($data['tree']);
  }

  /** * Returns a tree containing of MenuLinkTreeElement based upon tree data. * * This method converts the tree representation as array coming from the tree * storage to a tree containing a list of MenuLinkTreeElement[]. * * @param array $data_tree * The tree data coming from the menu tree storage. * * @return \Drupal\Core\Menu\MenuLinkTreeElement[] * An array containing the elements of a menu tree. */
$this->assertEquals(['PUT']$route->getMethods(), 'The right route method was found.');

    $exception_thrown = FALSE;
    try {
      $provider->getRouteByName('invalid_name');
    }
    catch (RouteNotFoundException $e) {
      $exception_thrown = TRUE;
    }
    $this->assertTrue($exception_thrown, 'Random route was not found.');

    $routes = $provider->getRoutesByNames(['route_c', 'route_d', $this->randomMachineName()]);
    $this->assertCount(2, $routes, 'Only two valid routes found.');
    $this->assertEquals('/path/two', $routes['route_c']->getPath());
    $this->assertEquals('/path/three', $routes['route_d']->getPath());
  }

  /** * Ensures that the routing system is capable of extreme long patterns. */
  public function testGetRoutesByPatternWithLongPatterns() {
    $connection = Database::getConnection();
    $provider = new TestRouteProvider($connection$this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');

    
/** * {@inheritdoc} */
  public function preLoadRoutes($names) {
    return $this->getRouteProvider()->preLoadRoutes($names);
  }

  /** * {@inheritdoc} */
  public function getRoutesByNames($names) {
    return $this->getRouteProvider()->getRoutesByNames($names);
  }

  /** * {@inheritdoc} */
  public function getRoutesByPattern($pattern) {
    return $this->getRouteProvider()->getRoutesByPattern($pattern);
  }

  /** * {@inheritdoc} */
// Collect all route names.     $route_names = [];
    foreach ($tree as $instances) {
      foreach ($instances as $child) {
        $route_names[] = $child->getRouteName();
      }
    }
    // Pre-fetch all routes involved in the tree. This reduces the number     // of SQL queries that would otherwise be triggered by the access manager.     if ($route_names) {
      $this->routeProvider->getRoutesByNames($route_names);
    }

    foreach ($tree as $level => $instances) {
      /** @var \Drupal\Core\Menu\LocalTaskInterface[] $instances */
      foreach ($instances as $plugin_id => $child) {
        $route_name = $child->getRouteName();
        $route_parameters = $child->getRouteParameters($this->routeMatch);

        // Given that the active flag depends on the route we have to add the         // route cache context.         $cacheability->addCacheContexts(['route']);
        

  public function getRouteCollectionForRequest(Request $request) {
    return $this->routes;
  }

  /** * {@inheritdoc} */
  public function getRouteByName($name) {
    $routes = $this->getRoutesByNames([$name]);
    if (empty($routes)) {
      throw new RouteNotFoundException(sprintf('Route "%s" does not exist.', $name));
    }

    return reset($routes);
  }

  /** * {@inheritdoc} */
  public function preLoadRoutes($names) {
    
/** * {@inheritdoc} */
  public function preLoadRoutes($names) {
    return $this->lazyLoadItself()->preLoadRoutes($names);
  }

  /** * {@inheritdoc} */
  public function getRoutesByNames($names) {
    return $this->lazyLoadItself()->getRoutesByNames($names);
  }

  /** * {@inheritdoc} */
  public function getCandidateOutlines(array $parts) {
    return $this->lazyLoadItself()->getCandidateOutlines($parts);
  }

  /** * {@inheritdoc} */
Home | Imprint | This part of the site doesn't use cookies.