本文整理汇总了PHP中Symfony\Component\Routing\RouteCollection::addPrefix方法的典型用法代码示例。如果您正苦于以下问题:PHP RouteCollection::addPrefix方法的具体用法?PHP RouteCollection::addPrefix怎么用?PHP RouteCollection::addPrefix使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\Routing\RouteCollection
的用法示例。
在下文中一共展示了RouteCollection::addPrefix方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: addCollection
/**
* 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());
}
示例2: testAddPrefix
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');
}
示例3: onKernelRequest
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);
}
示例4: load
/**
* {@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;
}
示例5: load
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;
}
示例6: loadRoutes
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);
}
示例7: addPrefix
/**
* {@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);
}
}
示例8: flush
/**
* 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;
}
示例9: addCollection
/**
* 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;
}
示例10: testAddPrefixOverridesDefaultsAndRequirements
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');
}
示例11: getRouteCollections
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);
//.........这里部分代码省略.........
示例12: initRouting
/**
* @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;
}
示例13: testCannotRelyOnPrefix
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'));
}
示例14: getRouteCollection
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;
}
示例15: addCollection
/**
* 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;
}