/** * Adds a route collection at the end of the current set by appending all * routes of the added collection. * * @param RouteCollection $collection A RouteCollection instance * * @api */ public function addCollection(RouteCollection $collection) { // This is to keep BC for getParent() and getRoot(). It does not prevent // infinite loops by recursive referencing. But we don't need that logic // anymore as the tree logic has been deprecated and we are just widening // the accepted range. $collection->parent = $this; // this is to keep BC $numargs = func_num_args(); if ($numargs > 1) { $collection->addPrefix($this->prefix . func_get_arg(1)); if ($numargs > 2) { $collection->addDefaults(func_get_arg(2)); if ($numargs > 3) { $collection->addRequirements(func_get_arg(3)); if ($numargs > 4) { $collection->addOptions(func_get_arg(4)); } } } } else { // the sub-collection must have the prefix of the parent (current instance) prepended because it does not // necessarily already have it applied (depending on the order RouteCollections are added to each other) // this will be removed when the BC layer for getPrefix() is removed $collection->addPrefix($this->prefix); } // we need to remove all routes with the same names first because just replacing them // would not place the new route at the end of the merged array foreach ($collection->all() as $name => $route) { unset($this->routes[$name]); $this->routes[$name] = $route; } $this->resources = array_merge($this->resources, $collection->getResources()); }
/** * @param ContainerInterface $container * @return UrlMatcher */ function initRouting(ContainerInterface $container) { $routeCollection = new RouteCollection(); $viewRoute = new Route('/view', ['_controller' => 'Crud', '_action' => 'view']); $routeCollection->add('crud_view', $viewRoute); $addRoute = new Route('/add', ['_controller' => 'Crud', '_action' => 'add']); $routeCollection->add('crud_add', $addRoute); $editRoute = new Route('/edit/{id}', ['_controller' => 'Crud', '_action' => 'edit'], ['id' => '.+']); $routeCollection->add('crud_edit', $editRoute); $routeCollection->addPrefix('/{_locale}/crud'); $routeCollection->addDefaults(['_locale' => 'ru']); $routeCollection->addRequirements(['_locale' => 'ru|en']); $locator = new FileLocator([SRC_PATH . 'Resources/config']); $loader = new YamlFileLoader($locator); $collection = $loader->load('routes.yml'); $routeCollection->addCollection($collection); $closureLoader = new ClosureLoader(); $collection = $closureLoader->load(function () { return new RouteCollection(); }); $requestContext = new RequestContext(); $requestContext->fromRequest(Request::createFromGlobals()); $matcher = new UrlMatcher($routeCollection, $requestContext); $container->set('url_matcher', $matcher); $urlGenerator = new UrlGenerator($routeCollection, $requestContext); $container->set('url_generator', $urlGenerator); return $matcher; }
public function testAddDefaultsAndRequirementsAndOptions() { $collection = new RouteCollection(); $collection->add('foo', new Route('/{placeholder}')); $collection1 = new RouteCollection(); $collection1->add('bar', new Route('/{placeholder}', array('_controller' => 'fixed', 'placeholder' => 'default'), array('placeholder' => '.+'), array('option' => 'value'))); $collection->addCollection($collection1); $collection->addDefaults(array('placeholder' => 'new-default')); $this->assertEquals(array('placeholder' => 'new-default'), $collection->get('foo')->getDefaults(), '->addDefaults() adds defaults to all routes'); $this->assertEquals(array('_controller' => 'fixed', 'placeholder' => 'new-default'), $collection->get('bar')->getDefaults(), '->addDefaults() adds defaults to all routes and overwrites existing ones'); $collection->addRequirements(array('placeholder' => '\\d+')); $this->assertEquals(array('placeholder' => '\\d+'), $collection->get('foo')->getRequirements(), '->addRequirements() adds requirements to all routes'); $this->assertEquals(array('placeholder' => '\\d+'), $collection->get('bar')->getRequirements(), '->addRequirements() adds requirements to all routes and overwrites existing ones'); $collection->addOptions(array('option' => 'new-value')); $this->assertEquals(array('option' => 'new-value', 'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler'), $collection->get('bar')->getOptions(), '->addOptions() adds options to all routes and overwrites existing ones'); }
/** * Adiciona wildcards padrões para serem utilizados na detecção de rotas por regex. */ private function addDefaultsWildCards() { $this->collection->addRequirements(['any' => '[a-zA-Z\\-\\.0-9]+\\=\\?']); }