You are here

public function ChainRouter::generate in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony-cmf/routing/ChainRouter.php \Symfony\Cmf\Component\Routing\ChainRouter::generate()

Loops through all registered routers and returns a router if one is found. It will always return the first route generated.

Overrides UrlGeneratorInterface::generate

File

vendor/symfony-cmf/routing/ChainRouter.php, line 214

Class

ChainRouter
The ChainRouter allows to combine several routers to try in a defined order.

Namespace

Symfony\Cmf\Component\Routing

Code

public function generate($name, $parameters = array(), $absolute = false) {
  $debug = array();
  foreach ($this
    ->all() as $router) {

    // if $router does not announce it is capable of handling
    // non-string routes and $name is not a string, continue
    if ($name && !is_string($name) && !$router instanceof VersatileGeneratorInterface) {
      continue;
    }

    // If $router is versatile and doesn't support this route name, continue
    if ($router instanceof VersatileGeneratorInterface && !$router
      ->supports($name)) {
      continue;
    }
    try {
      return $router
        ->generate($name, $parameters, $absolute);
    } catch (RouteNotFoundException $e) {
      $hint = $this
        ->getErrorMessage($name, $router, $parameters);
      $debug[] = $hint;
      if ($this->logger) {
        $this->logger
          ->debug('Router ' . get_class($router) . " was unable to generate route. Reason: '{$hint}': " . $e
          ->getMessage());
      }
    }
  }
  if ($debug) {
    $debug = array_unique($debug);
    $info = implode(', ', $debug);
  }
  else {
    $info = $this
      ->getErrorMessage($name);
  }
  throw new RouteNotFoundException(sprintf('None of the chained routers were able to generate route: %s', $info));
}