public function testAddPrefix() { $collection = new RouteCollection(); $collection->addRoute('foo', $foo = new Route('/foo')); $collection->addRoute('bar', $bar = new Route('/bar')); $collection->addPrefix('/admin'); $this->assertEquals('/admin/foo', $collection->getRoute('foo')->getPattern(), '->addPrefix() adds a prefix to all routes'); $this->assertEquals('/admin/bar', $collection->getRoute('bar')->getPattern(), '->addPrefix() adds a prefix to all routes'); }
public function testNormalizeUrl() { $collection = new RouteCollection(); $collection->addRoute('foo', new Route('/:foo')); $matcher = new UrlMatcherForTests($collection, array(), array()); $this->assertEquals('/', $matcher->normalizeUrl(''), '->normalizeUrl() adds a / at the beginning of the URL if needed'); $this->assertEquals('/foo', $matcher->normalizeUrl('foo'), '->normalizeUrl() adds a / at the beginning of the URL if needed'); $this->assertEquals('/foo', $matcher->normalizeUrl('/foo?foo=bar'), '->normalizeUrl() removes the query string'); $this->assertEquals('/foo/bar', $matcher->normalizeUrl('/foo//bar'), '->normalizeUrl() removes duplicated /'); }
/** * @throws \InvalidArgumentException When config pattern is not defined for the given route */ protected function parseRoute(RouteCollection $collection, $name, $config, $file) { $defaults = isset($config['defaults']) ? $config['defaults'] : array(); $requirements = isset($config['requirements']) ? $config['requirements'] : array(); $options = isset($config['options']) ? $config['options'] : array(); if (!isset($config['pattern'])) { throw new \InvalidArgumentException(sprintf('You must define a "pattern" for the "%s" route.', $name)); } $route = new Route($config['pattern'], $defaults, $requirements, $options); $collection->addRoute($name, $route); }
/** * @covers Symfony\Component\Routing\Loader\ClosureLoader::load */ public function testLoad() { $loader = new ClosureLoader(); $route = new Route('/'); $routes = $loader->load(function () use($route) { $routes = new RouteCollection(); $routes->addRoute('foo', $route); return $routes; }); $this->assertEquals($route, $routes->getRoute('foo'), '->load() loads a \\Closure resource'); }
/** * @covers Symfony\Component\Routing\Loader\DelegatingLoader::load */ public function testLoad() { $resolver = new LoaderResolver(array(new ClosureLoader())); $loader = new DelegatingLoader($resolver); $route = new Route('/'); $routes = $loader->load(function () use($route) { $routes = new RouteCollection(); $routes->addRoute('foo', $route); return $routes; }); $this->assertSame($route, $routes->getRoute('foo'), '->load() loads a resource using the loaders from the resolver'); try { $loader->load('foo.foo'); $this->fail('->load() throws an \\InvalidArgumentException if the resource cannot be loaded'); } catch (\Exception $e) { $this->assertInstanceOf('\\InvalidArgumentException', $e, '->load() throws an \\InvalidArgumentException if the resource cannot be loaded'); } }
<?php use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\Route; $collection = new RouteCollection(); $collection->addRoute('homepage', new Route('/', array('_controller' => 'FrameworkBundle:Default:index'))); return $collection;
/** * Loads from annotations from a class. * * @param string $class A class name * * @return RouteCollection A RouteCollection instance * * @throws \InvalidArgumentException When route can't be parsed */ public function load($class) { if (!class_exists($class)) { throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class)); } $class = new \ReflectionClass($class); $annotClass = 'Symfony\\Component\\Routing\\Annotation\\Route'; $globals = array('pattern' => '', 'requirements' => array(), 'options' => array(), 'defaults' => array()); if ($annot = $this->reader->getClassAnnotation($class, $annotClass)) { if (null !== $annot->getPattern()) { $globals['pattern'] = $annot->getPattern(); } if (null !== $annot->getRequirements()) { $globals['requirements'] = $annot->getRequirements(); } if (null !== $annot->getOptions()) { $globals['options'] = $annot->getOptions(); } if (null !== $annot->getDefaults()) { $globals['defaults'] = $annot->getDefaults(); } } $this->reader->setDefaultAnnotationNamespace('Symfony\\Component\\Routing\\Annotation\\'); $collection = new RouteCollection(); foreach ($class->getMethods() as $method) { if ($annot = $this->reader->getMethodAnnotation($method, $annotClass)) { if (null === $annot->getName()) { $annot->setName($this->getDefaultRouteName($class, $method)); } $defaults = array_merge($globals['defaults'], $annot->getDefaults(), $this->getRouteDefaults($class, $method, $annot)); $requirements = array_merge($globals['requirements'], $annot->getRequirements()); $options = array_merge($globals['options'], $annot->getOptions()); $route = new Route($globals['pattern'] . $annot->getPattern(), $defaults, $requirements, $options); $collection->addRoute($annot->getName(), $route); } } return $collection; }
<?php use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\Route; $collection = new RouteCollection(); $collection->addRoute('hello', new Route('/hello/:name', array('_controller' => 'HelloBundle:Hello:index'))); return $collection;
protected function parseRoute(RouteCollection $collection, $definition, $file) { $defaults = array(); $requirements = array(); $options = array(); foreach ($definition->childNodes as $node) { if (!$node instanceof \DOMElement) { continue; } switch ($node->tagName) { case 'default': $defaults[(string) $node->getAttribute('key')] = trim((string) $node->nodeValue); break; case 'option': $options[(string) $node->getAttribute('key')] = trim((string) $node->nodeValue); break; case 'requirement': $requirements[(string) $node->getAttribute('key')] = trim((string) $node->nodeValue); break; default: throw new \InvalidArgumentException(sprintf('Unable to parse tag "%s"', $node->tagName)); } } $route = new Route((string) $definition->getAttribute('pattern'), $defaults, $requirements, $options); $collection->addRoute((string) $definition->getAttribute('id'), $route); }