Symfony\Component\Routing\Router::getGenerator PHP Method

getGenerator() public method

Gets the UrlGenerator instance associated with this Router.
public getGenerator ( ) : UrlGeneratorInterface
return UrlGeneratorInterface A UrlGeneratorInterface instance
    public function getGenerator()
    {
        if (null !== $this->generator) {
            return $this->generator;
        }

        if (null === $this->options['cache_dir'] || null === $this->options['generator_cache_class']) {
            return $this->generator = new $this->options['generator_class']($this->getRouteCollection(), $this->context, $this->defaults);
        }

        $class = $this->options['generator_cache_class'];
        $cache = new ConfigCache($this->options['cache_dir'].'/'.$class.'.php', $this->options['debug']);
        if (!$cache->isFresh($class)) {
            $dumper = new $this->options['generator_dumper_class']($this->getRouteCollection());

            $options = array(
                'class'      => $class,
                'base_class' => $this->options['generator_base_class'],
            );

            $cache->write($dumper->dump($options), $this->getRouteCollection()->getResources());
        }

        require_once $cache;

        return $this->generator = new $class($this->context, $this->defaults);
    }

Usage Example

 /**
  * @param RouterInterface $router
  */
 public function __construct(Router $router)
 {
     /**
      * @var UrlGenerator $urlGenerator
      */
     $urlGenerator = $router->getGenerator();
     $this->urlGenerator = $urlGenerator;
 }
All Usage Examples Of Symfony\Component\Routing\Router::getGenerator