The Router class is an example of the integration of all pieces of the routing system for easier use.
@author Fabien Potencier <fabien@symfony.com>
Expanded class hierarchy of Router
class Router implements RouterInterface {
/**
* @var UrlMatcherInterface|null
*/
protected $matcher;
/**
* @var UrlGeneratorInterface|null
*/
protected $generator;
/**
* @var RequestContext
*/
protected $context;
/**
* @var LoaderInterface
*/
protected $loader;
/**
* @var RouteCollection|null
*/
protected $collection;
/**
* @var mixed
*/
protected $resource;
/**
* @var array
*/
protected $options = array();
/**
* @var LoggerInterface|null
*/
protected $logger;
/**
* Constructor.
*
* @param LoaderInterface $loader A LoaderInterface instance
* @param mixed $resource The main resource to load
* @param array $options An array of options
* @param RequestContext $context The context
* @param LoggerInterface $logger A logger instance
*/
public function __construct(LoaderInterface $loader, $resource, array $options = array(), RequestContext $context = null, LoggerInterface $logger = null) {
$this->loader = $loader;
$this->resource = $resource;
$this->logger = $logger;
$this->context = null === $context ? new RequestContext() : $context;
$this
->setOptions($options);
}
/**
* Sets options.
*
* Available options:
*
* * cache_dir: The cache directory (or null to disable caching)
* * debug: Whether to enable debugging or not (false by default)
* * resource_type: Type hint for the main resource (optional)
*
* @param array $options An array of options
*
* @throws \InvalidArgumentException When unsupported option is provided
*/
public function setOptions(array $options) {
$this->options = array(
'cache_dir' => null,
'debug' => false,
'generator_class' => 'Symfony\\Component\\Routing\\Generator\\UrlGenerator',
'generator_base_class' => 'Symfony\\Component\\Routing\\Generator\\UrlGenerator',
'generator_dumper_class' => 'Symfony\\Component\\Routing\\Generator\\Dumper\\PhpGeneratorDumper',
'generator_cache_class' => 'ProjectUrlGenerator',
'matcher_class' => 'Symfony\\Component\\Routing\\Matcher\\UrlMatcher',
'matcher_base_class' => 'Symfony\\Component\\Routing\\Matcher\\UrlMatcher',
'matcher_dumper_class' => 'Symfony\\Component\\Routing\\Matcher\\Dumper\\PhpMatcherDumper',
'matcher_cache_class' => 'ProjectUrlMatcher',
'resource_type' => null,
'strict_requirements' => true,
);
// check option names and live merge, if errors are encountered Exception will be thrown
$invalid = array();
foreach ($options as $key => $value) {
if (array_key_exists($key, $this->options)) {
$this->options[$key] = $value;
}
else {
$invalid[] = $key;
}
}
if ($invalid) {
throw new \InvalidArgumentException(sprintf('The Router does not support the following options: "%s".', implode('", "', $invalid)));
}
}
/**
* Sets an option.
*
* @param string $key The key
* @param mixed $value The value
*
* @throws \InvalidArgumentException
*/
public function setOption($key, $value) {
if (!array_key_exists($key, $this->options)) {
throw new \InvalidArgumentException(sprintf('The Router does not support the "%s" option.', $key));
}
$this->options[$key] = $value;
}
/**
* Gets an option value.
*
* @param string $key The key
*
* @return mixed The value
*
* @throws \InvalidArgumentException
*/
public function getOption($key) {
if (!array_key_exists($key, $this->options)) {
throw new \InvalidArgumentException(sprintf('The Router does not support the "%s" option.', $key));
}
return $this->options[$key];
}
/**
* {@inheritdoc}
*/
public function getRouteCollection() {
if (null === $this->collection) {
$this->collection = $this->loader
->load($this->resource, $this->options['resource_type']);
}
return $this->collection;
}
/**
* {@inheritdoc}
*/
public function setContext(RequestContext $context) {
$this->context = $context;
if (null !== $this->matcher) {
$this
->getMatcher()
->setContext($context);
}
if (null !== $this->generator) {
$this
->getGenerator()
->setContext($context);
}
}
/**
* {@inheritdoc}
*/
public function getContext() {
return $this->context;
}
/**
* {@inheritdoc}
*/
public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH) {
return $this
->getGenerator()
->generate($name, $parameters, $referenceType);
}
/**
* {@inheritdoc}
*/
public function match($pathinfo) {
return $this
->getMatcher()
->match($pathinfo);
}
/**
* Gets the UrlMatcher instance associated with this Router.
*
* @return UrlMatcherInterface A UrlMatcherInterface instance
*/
public function getMatcher() {
if (null !== $this->matcher) {
return $this->matcher;
}
if (null === $this->options['cache_dir'] || null === $this->options['matcher_cache_class']) {
return $this->matcher = new $this->options['matcher_class']($this
->getRouteCollection(), $this->context);
}
$class = $this->options['matcher_cache_class'];
$cache = new ConfigCache($this->options['cache_dir'] . '/' . $class . '.php', $this->options['debug']);
if (!$cache
->isFresh($class)) {
$dumper = new $this->options['matcher_dumper_class']($this
->getRouteCollection());
$options = array(
'class' => $class,
'base_class' => $this->options['matcher_base_class'],
);
$cache
->write($dumper
->dump($options), $this
->getRouteCollection()
->getResources());
}
require_once $cache;
return $this->matcher = new $class($this->context);
}
/**
* Gets the UrlGenerator instance associated with this Router.
*
* @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']) {
$this->generator = new $this->options['generator_class']($this
->getRouteCollection(), $this->context, $this->logger);
}
else {
$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;
$this->generator = new $class($this->context, $this->logger);
}
if ($this->generator instanceof ConfigurableRequirementsInterface) {
$this->generator
->setStrictRequirements($this->options['strict_requirements']);
}
return $this->generator;
}
}
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Router:: |
protected | property | ||
Router:: |
protected | property | ||
Router:: |
protected | property | ||
Router:: |
protected | property | ||
Router:: |
protected | property | ||
Router:: |
protected | property | ||
Router:: |
protected | property | ||
Router:: |
protected | property | ||
Router:: |
public | function |
Generates a URL or path for a specific route based on the given parameters. Overrides UrlGeneratorInterface:: |
|
Router:: |
public | function |
Gets the request context. Overrides RequestContextAwareInterface:: |
|
Router:: |
public | function | Gets the UrlGenerator instance associated with this Router. | |
Router:: |
public | function | Gets the UrlMatcher instance associated with this Router. | |
Router:: |
public | function | Gets an option value. | |
Router:: |
public | function |
Gets the RouteCollection instance associated with this Router. Overrides RouterInterface:: |
|
Router:: |
public | function |
Tries to match a URL path with a set of routes. Overrides UrlMatcherInterface:: |
|
Router:: |
public | function |
Sets the request context. Overrides RequestContextAwareInterface:: |
|
Router:: |
public | function | Sets an option. | |
Router:: |
public | function | Sets options. | |
Router:: |
public | function | Constructor. | |
UrlGeneratorInterface:: |
constant | Generates an absolute path, e.g. "/dir/file". | ||
UrlGeneratorInterface:: |
constant | Generates an absolute URL, e.g. "http://example.com/dir/file". | ||
UrlGeneratorInterface:: |
constant | Generates a network path, e.g. "//example.com/dir/file". Such reference reuses the current scheme but specifies the host. | ||
UrlGeneratorInterface:: |
constant | Generates a relative path based on the current request path, e.g. "../parent-file". |