addPrefix() public method

Adds a prefix to all routes in the current set.
public addPrefix ( string $prefix )
$prefix string An optional prefix to add before each pattern of the route collection
Exemplo n.º 1
0
 /**
  * Adds a route collection to the current set of routes (at the end of the current set).
  *
  * @param RouteCollection $collection A RouteCollection instance
  * @param string          $prefix     An optional prefix to add before each pattern of the route collection
  */
 public function addCollection(RouteCollection $collection, $prefix = '')
 {
     $collection->addPrefix($prefix);
     foreach ($collection->getResources() as $resource) {
         $this->addResource($resource);
     }
     $this->routes = array_merge($this->routes, $collection->all());
 }
Exemplo n.º 2
0
 public function testAddPrefix()
 {
     $collection = new RouteCollection();
     $collection->add('foo', $foo = new Route('/foo'));
     $collection->add('bar', $bar = new Route('/bar'));
     $collection->addPrefix('/admin');
     $this->assertEquals('/admin/foo', $collection->get('foo')->getPattern(), '->addPrefix() adds a prefix to all routes');
     $this->assertEquals('/admin/bar', $collection->get('bar')->getPattern(), '->addPrefix() adds a prefix to all routes');
 }
Exemplo n.º 3
0
 public function onKernelRequest(GetResponseEvent $event)
 {
     $collection = new RouteCollection();
     $collection->addPrefix('en');
     $collection->add('test', new Route('/hello', array('controller' => 'AppBundle/Controller/HelloController', 'action' => 'index')));
     $secondCollection = new RouteCollection();
     $secondCollection->addPrefix('prod');
     $collection->addCollection($secondCollection);
 }
 /**
  * {@inheritdoc}
  */
 public function load($resource, $type = null)
 {
     if (true === $this->loaded) {
         throw new RuntimeException('load(): Do not add this loader twice.');
     }
     $this->generatesRoutes();
     $this->generateAdminHomeRoute();
     $this->routes->addPrefix($this->adminRoutePrefix);
     return $this->routes;
 }
Exemplo n.º 5
0
 public function load($resource, $type = null)
 {
     $routes = new RouteCollection();
     $nRoutes = $this->nRouteManager->getNestedRoutes();
     foreach ($nRoutes as $nRoute) {
         $flattenRoutes = $this->flattenRouteFactory->getFlattenRoutes($nRoute);
         $this->fRouteManager->addFlattenRoute($flattenRoutes);
         $registerRoutes = $this->flattenRouteFactory->getRoutes($flattenRoutes);
         foreach ($registerRoutes as $key => $route) {
             $routes->add($key, $route);
         }
         $reflection = new ReflectionObject($nRoute);
         $routes->addResource(new FileResource($reflection->getFileName()));
     }
     $routes->addPrefix($this->rootPath);
     return $routes;
 }
Exemplo n.º 6
0
 protected function loadRoutes(RouteCollection $routes = null)
 {
     $routes = $this->beforeLoadRoutes($routes);
     if ($routes) {
         $this->routes = $routes;
     } else {
         $this->routes = new RouteCollection();
         foreach ($this->getComponentsRoutes() as $name => $routes) {
             $this->routes->addCollection($routes);
         }
         $this->routes->addCollection($this->getRootRoutes());
     }
     //var_dump(BASE_URL);
     if (BASE_URL != '/') {
         //добавляем префикс /админ
         $this->routes->addPrefix(BASE_URL);
         //die('ok');
     }
     $this->afterLoadRoutes($this->routes);
     //echo '<pre>';var_dump($this->routes);
 }
 /**
  * {@inheritdoc}
  */
 public function addPrefix($prefix, array $defaults = array(), array $requirements = array())
 {
     $originalPrefix = false;
     if (is_array($prefix)) {
         foreach ($prefix as $locale => $localePrefix) {
             $prefix[$locale] = trim(trim($localePrefix), '/');
         }
         $this->localizeRoutes(array_keys($prefix));
     } elseif (is_string($prefix) && preg_match(self::LOCALE_REGEX, $prefix)) {
         $originalPrefix = trim(trim($prefix), '/');
         $prefix = array();
     } else {
         parent::addPrefix($prefix, $defaults, $requirements);
         return;
     }
     foreach ($this->all() as $name => $route) {
         $locale = $route->getDefault(self::LOCALE_PARAM);
         $routePrefix = $this->localizePrefix($locale, $prefix, $originalPrefix, $name);
         $route->setPath('/' . $routePrefix . $route->getPath());
         $route->addDefaults($defaults);
         $route->addRequirements($requirements);
     }
 }
 /**
  * Persists and freezes staged controllers.
  *
  * @param string $prefix
  *
  * @return RouteCollection A RouteCollection instance
  */
 public function flush($prefix = '')
 {
     $routes = new RouteCollection();
     foreach ($this->controllers as $controller) {
         if (!($name = $controller->getRouteName())) {
             $name = $controller->generateRouteName($prefix);
             while ($routes->get($name)) {
                 $name .= '_';
             }
             $controller->bind($name);
         }
         $routes->add($name, $controller->getRoute());
         $controller->freeze();
     }
     $routes->addPrefix($prefix);
     $this->controllers = array();
     return $routes;
 }
Exemplo n.º 9
0
    /**
     * Adds a route collection to the current set of routes (at the end of the current set).
     *
     * @param RouteCollection $collection A RouteCollection instance
     * @param string          $prefix     An optional prefix to add before each pattern of the route collection
     */
    public function addCollection(RouteCollection $collection, $prefix = '')
    {
        $collection->addPrefix($prefix);

        $this->routes[] = $collection;
    }
Exemplo n.º 10
0
 public function testAddPrefixOverridesDefaultsAndRequirements()
 {
     $collection = new RouteCollection();
     $collection->add('foo', $foo = new Route('/foo'));
     $collection->add('bar', $bar = new Route('/bar', array(), array('_scheme' => 'http')));
     $collection->addPrefix('/admin', array(), array('_scheme' => 'https'));
     $this->assertEquals('https', $collection->get('foo')->getRequirement('_scheme'), '->addPrefix() overrides existing requirements');
     $this->assertEquals('https', $collection->get('bar')->getRequirement('_scheme'), '->addPrefix() overrides existing requirements');
 }
 public function getRouteCollections()
 {
     /* test case 1 */
     $collection = new RouteCollection();
     $collection->add('overridden', new Route('/overridden'));
     // defaults and requirements
     $collection->add('foo', new Route('/foo/{bar}', array('def' => 'test'), array('bar' => 'baz|symfony')));
     // method requirement
     $collection->add('bar', new Route('/bar/{foo}', array(), array('_method' => 'GET|head')));
     // GET method requirement automatically adds HEAD as valid
     $collection->add('barhead', new Route('/barhead/{foo}', array(), array('_method' => 'GET')));
     // simple
     $collection->add('baz', new Route('/test/baz'));
     // simple with extension
     $collection->add('baz2', new Route('/test/baz.html'));
     // trailing slash
     $collection->add('baz3', new Route('/test/baz3/'));
     // trailing slash with variable
     $collection->add('baz4', new Route('/test/{foo}/'));
     // trailing slash and method
     $collection->add('baz5', new Route('/test/{foo}/', array(), array('_method' => 'post')));
     // complex name
     $collection->add('baz.baz6', new Route('/test/{foo}/', array(), array('_method' => 'put')));
     // defaults without variable
     $collection->add('foofoo', new Route('/foofoo', array('def' => 'test')));
     // pattern with quotes
     $collection->add('quoter', new Route('/{quoter}', array(), array('quoter' => '[\']+')));
     // space in pattern
     $collection->add('space', new Route('/spa ce'));
     // prefixes
     $collection1 = new RouteCollection();
     $collection1->add('overridden', new Route('/overridden1'));
     $collection1->add('foo1', new Route('/{foo}'));
     $collection1->add('bar1', new Route('/{bar}'));
     $collection1->addPrefix('/b\'b');
     $collection2 = new RouteCollection();
     $collection2->addCollection($collection1);
     $collection2->add('overridden', new Route('/{var}', array(), array('var' => '.*')));
     $collection1 = new RouteCollection();
     $collection1->add('foo2', new Route('/{foo1}'));
     $collection1->add('bar2', new Route('/{bar1}'));
     $collection1->addPrefix('/b\'b');
     $collection2->addCollection($collection1);
     $collection2->addPrefix('/a');
     $collection->addCollection($collection2);
     // overridden through addCollection() and multiple sub-collections with no own prefix
     $collection1 = new RouteCollection();
     $collection1->add('overridden2', new Route('/old'));
     $collection1->add('helloWorld', new Route('/hello/{who}', array('who' => 'World!')));
     $collection2 = new RouteCollection();
     $collection3 = new RouteCollection();
     $collection3->add('overridden2', new Route('/new'));
     $collection3->add('hey', new Route('/hey/'));
     $collection2->addCollection($collection3);
     $collection1->addCollection($collection2);
     $collection1->addPrefix('/multi');
     $collection->addCollection($collection1);
     // "dynamic" prefix
     $collection1 = new RouteCollection();
     $collection1->add('foo3', new Route('/{foo}'));
     $collection1->add('bar3', new Route('/{bar}'));
     $collection1->addPrefix('/b');
     $collection1->addPrefix('{_locale}');
     $collection->addCollection($collection1);
     // route between collections
     $collection->add('ababa', new Route('/ababa'));
     // collection with static prefix but only one route
     $collection1 = new RouteCollection();
     $collection1->add('foo4', new Route('/{foo}'));
     $collection1->addPrefix('/aba');
     $collection->addCollection($collection1);
     // prefix and host
     $collection1 = new RouteCollection();
     $route1 = new Route('/route1', array(), array(), array(), 'a.example.com');
     $collection1->add('route1', $route1);
     $collection2 = new RouteCollection();
     $route2 = new Route('/c2/route2', array(), array(), array(), 'a.example.com');
     $collection1->add('route2', $route2);
     $route3 = new Route('/c2/route3', array(), array(), array(), 'b.example.com');
     $collection1->add('route3', $route3);
     $route4 = new Route('/route4', array(), array(), array(), 'a.example.com');
     $collection1->add('route4', $route4);
     $route5 = new Route('/route5', array(), array(), array(), 'c.example.com');
     $collection1->add('route5', $route5);
     $route6 = new Route('/route6', array(), array(), array(), null);
     $collection1->add('route6', $route6);
     $collection->addCollection($collection1);
     // host and variables
     $collection1 = new RouteCollection();
     $route11 = new Route('/route11', array(), array(), array(), '{var1}.example.com');
     $collection1->add('route11', $route11);
     $route12 = new Route('/route12', array('var1' => 'val'), array(), array(), '{var1}.example.com');
     $collection1->add('route12', $route12);
     $route13 = new Route('/route13/{name}', array(), array(), array(), '{var1}.example.com');
     $collection1->add('route13', $route13);
     $route14 = new Route('/route14/{name}', array('var1' => 'val'), array(), array(), '{var1}.example.com');
     $collection1->add('route14', $route14);
     $route15 = new Route('/route15/{name}', array(), array(), array(), 'c.example.com');
     $collection1->add('route15', $route15);
     $route16 = new Route('/route16/{name}', array('var1' => 'val'), array(), array(), null);
     $collection1->add('route16', $route16);
     $route17 = new Route('/route17', array(), array(), array(), null);
     $collection1->add('route17', $route17);
     $collection->addCollection($collection1);
     // multiple sub-collections with a single route and a prefix each
     $collection1 = new RouteCollection();
     $collection1->add('a', new Route('/a...'));
     $collection2 = new RouteCollection();
     $collection2->add('b', new Route('/{var}'));
     $collection3 = new RouteCollection();
     $collection3->add('c', new Route('/{var}'));
     $collection3->addPrefix('/c');
     $collection2->addCollection($collection3);
     $collection2->addPrefix('/b');
     $collection1->addCollection($collection2);
     $collection1->addPrefix('/a');
     $collection->addCollection($collection1);
     /* test case 2 */
     $redirectCollection = clone $collection;
     // force HTTPS redirection
     $redirectCollection->add('secure', new Route('/secure', array(), array('_scheme' => 'https')));
     // force HTTP redirection
     $redirectCollection->add('nonsecure', new Route('/nonsecure', array(), array('_scheme' => 'http')));
     /* test case 3 */
     $rootprefixCollection = new RouteCollection();
     $rootprefixCollection->add('static', new Route('/test'));
     $rootprefixCollection->add('dynamic', new Route('/{var}'));
     $rootprefixCollection->addPrefix('rootprefix');
     $route = new Route('/with-condition');
     $route->setCondition('context.getMethod() == "GET"');
     $rootprefixCollection->add('with-condition', $route);
     return array(array($collection, 'url_matcher1.php', array()), array($redirectCollection, 'url_matcher2.php', array('base_class' => 'Symfony\\Component\\Routing\\Tests\\Fixtures\\RedirectableUrlMatcher')), array($rootprefixCollection, 'url_matcher3.php', array()));
 }
/**
 * @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 testCannotRelyOnPrefix()
 {
     $coll = new RouteCollection();
     $subColl = new RouteCollection();
     $subColl->add('bar', new Route('/bar'));
     $subColl->addPrefix('/prefix');
     // overwrite the pattern, so the prefix is not valid anymore for this route in the collection
     $subColl->get('bar')->setPattern('/new');
     $coll->addCollection($subColl);
     $matcher = new UrlMatcher($coll, new RequestContext());
     $this->assertEquals(array('_route' => 'bar'), $matcher->match('/new'));
 }
 private function getRouteCollection()
 {
     $collection = new RouteCollection();
     // defaults and requirements
     $collection->add('foo', new Route('/foo/{bar}', array('def' => 'test'), array('bar' => 'baz|symfony')));
     // defaults parameters in pattern
     $collection->add('foobar', new Route('/foo/{bar}', array('bar' => 'toto')));
     // method requirement
     $collection->add('bar', new Route('/bar/{foo}', array(), array('_method' => 'GET|head')));
     // method requirement (again)
     $collection->add('baragain', new Route('/baragain/{foo}', array(), array('_method' => 'get|post')));
     // simple
     $collection->add('baz', new Route('/test/baz'));
     // simple with extension
     $collection->add('baz2', new Route('/test/baz.html'));
     // trailing slash
     $collection->add('baz3', new Route('/test/baz3/'));
     // trailing slash with variable
     $collection->add('baz4', new Route('/test/{foo}/'));
     // trailing slash and safe method
     $collection->add('baz5', new Route('/test/{foo}/', array(), array('_method' => 'get')));
     // trailing slash and unsafe method
     $collection->add('baz5unsafe', new Route('/testunsafe/{foo}/', array(), array('_method' => 'post')));
     // complex
     $collection->add('baz6', new Route('/test/baz', array('foo' => 'bar baz')));
     // space in path
     $collection->add('baz7', new Route('/te st/baz'));
     // space preceded with \ in path
     $collection->add('baz8', new Route('/te\\ st/baz'));
     // space preceded with \ in requirement
     $collection->add('baz9', new Route('/test/{baz}', array(), array('baz' => 'te\\\\ st')));
     $collection1 = new RouteCollection();
     $route1 = new Route('/route1', array(), array(), array(), 'a.example.com');
     $collection1->add('route1', $route1);
     $collection2 = new RouteCollection();
     $route2 = new Route('/route2', array(), array(), array(), 'a.example.com');
     $collection2->add('route2', $route2);
     $route3 = new Route('/route3', array(), array(), array(), 'b.example.com');
     $collection2->add('route3', $route3);
     $collection2->addPrefix('/c2');
     $collection1->addCollection($collection2);
     $route4 = new Route('/route4', array(), array(), array(), 'a.example.com');
     $collection1->add('route4', $route4);
     $route5 = new Route('/route5', array(), array(), array(), 'c.example.com');
     $collection1->add('route5', $route5);
     $route6 = new Route('/route6', array(), array(), array(), null);
     $collection1->add('route6', $route6);
     $collection->addCollection($collection1);
     // host and variables
     $collection1 = new RouteCollection();
     $route11 = new Route('/route11', array(), array(), array(), '{var1}.example.com');
     $collection1->add('route11', $route11);
     $route12 = new Route('/route12', array('var1' => 'val'), array(), array(), '{var1}.example.com');
     $collection1->add('route12', $route12);
     $route13 = new Route('/route13/{name}', array(), array(), array(), '{var1}.example.com');
     $collection1->add('route13', $route13);
     $route14 = new Route('/route14/{name}', array('var1' => 'val'), array(), array(), '{var1}.example.com');
     $collection1->add('route14', $route14);
     $route15 = new Route('/route15/{name}', array(), array(), array(), 'c.example.com');
     $collection1->add('route15', $route15);
     $route16 = new Route('/route16/{name}', array('var1' => 'val'), array(), array(), null);
     $collection1->add('route16', $route16);
     $route17 = new Route('/route17', array(), array(), array(), null);
     $collection1->add('route17', $route17);
     $collection->addCollection($collection1);
     return $collection;
 }
Exemplo n.º 15
0
 public function testAddPrefixOverridesDefaultsAndRequirements()
 {
     $collection = new RouteCollection();
     $collection->add('foo', $foo = new Route('/foo.{_format}'));
     $collection->add('bar', $bar = new Route('/bar.{_format}', array(), array('_format' => 'json')));
     $collection->addPrefix('/Admin', array(), array('_format' => 'html'));
     $this->assertEquals('html', $collection->get('foo')->getRequirement('_format'), '->addPrefix() overrides existing requirements');
     $this->assertEquals('html', $collection->get('bar')->getRequirement('_format'), '->addPrefix() overrides existing requirements');
 }
Exemplo n.º 16
0
 /**
  * Adds a route collection to the current set of routes (at the end of the current set).
  *
  * @param RouteCollection $collection A RouteCollection instance
  * @param string          $prefix     An optional prefix to add before each pattern of the route collection
  *
  * @api
  */
 public function addCollection(RouteCollection $collection, $prefix = '')
 {
     $collection->setParent($this);
     $collection->addPrefix($prefix);
     // remove all routes with the same name in all existing collections
     foreach (array_keys($collection->all()) as $name) {
         $this->remove($name);
     }
     $this->routes[] = $collection;
 }
 /**
  * {@inheritdoc}
  */
 public function addModularPrefix(RouteCollection $routes)
 {
     $routes->addPrefix('/{module}', [], ['module' => '[^/]+']);
 }
Exemplo n.º 18
0
<?php

// RouteProvider.php
use App\Controller;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
const DOMAIN_DEV = '\\w+.phoeni.x';
const DOMAIN_STAGE = 'stage.phoenix-vxtool.net';
const DOMAIN_LIVE = 'phoenix-vxtool.net';
const SUBDOMAIN_WWW = '|www.';
const SUBDOMAIN_API = 'api.';
$domain = implode('|', [DOMAIN_LIVE, DOMAIN_STAGE, DOMAIN_DEV]);
$routingConf = ['web-gui' => ['domain' => $domain, 'subdomain' => SUBDOMAIN_WWW, 'routes' => ['root' => ['methods' => ['GET'], 'path' => '/', 'controller' => [Controller\Web\Gui\Index::class, 'index']], 'get-logout' => ['methods' => ['GET'], 'path' => '/logout', 'controller' => [Controller\Web\Gui\Index::class, 'getLogout']], 'get-login' => ['methods' => ['GET'], 'path' => '/login', 'controller' => [Controller\Web\Gui\Index::class, 'getLogin']], 'post-login' => ['methods' => ['POST'], 'path' => '/login', 'controller' => [Controller\Web\Gui\Index::class, 'postLogin']]]], 'web-dev' => ['domain' => $domain, 'subdomain' => SUBDOMAIN_WWW, 'path' => '/dev', 'routes' => ['test' => ['methods' => ['GET'], 'path' => '/test{developerShort}', 'controller' => [Controller\Web\Dev\DevelopmentController::class, 'test'], 'template' => '']]]];
/**
 * @todo put following part into \App\Routing\Loader\ArrayFileLoader
 */
$masterCollection = new RouteCollection();
foreach ($routingConf as $groupName => $groupConfig) {
    $collection = new RouteCollection();
    $collection->setHost($groupConfig['subdomain'] . $groupConfig['domain']);
    foreach ($groupConfig['routes'] as $routeName => $route) {
        $defaults = array_replace_recursive(isset($route['defaults']) ? $route['defaults'] : [], ['_controller' => $route['controller'][0] . '::' . $route['controller'][1]]);
        $collection->add($groupName . '-' . $routeName, new Route($route['path'], $defaults, isset($route['requirements']) ? $route['requirements'] : [], isset($route['options']) ? $route['options'] : [], '', isset($route['schemes']) ? $route['schemes'] : [], isset($route['methods']) ? $route['methods'] : ['GET'], ''));
    }
    if (isset($groupConfig['path'])) {
        $collection->addPrefix($groupConfig['path']);
    }
    $masterCollection->addCollection($collection);
}
return $masterCollection;
    /**
     * 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());
    }
Exemplo n.º 20
0
 public function getAdminRoutesCollection($prefix)
 {
     $pool = $this->container->get('sonata.admin.pool');
     $collection = new RouteCollection();
     foreach ($pool->getAdminServiceIds() as $id) {
         $admin = $pool->getInstance($id);
         foreach ($admin->getRoutes()->getElements() as $code => $route) {
             $collection->add($route->getDefault('_sonata_name'), $route);
         }
         $reflection = new \ReflectionObject($admin);
         $collection->addResource(new FileResource($reflection->getFileName()));
     }
     $collection->addPrefix($prefix);
     return $collection;
 }