8000 [Routing] Allow to set name prefixes from the configuration by sroze · Pull Request #25178 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Routing] Allow to set name prefixes from the configuration #25178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,14 @@ final public function controller($controller)

return $this;
}

/**
* Adds a prefix to the name of all the routes within the collection.
*/
final public function addNamePrefix(string $prefix): self
{
$this->route->addNamePrefix($prefix);

return $this;
}
}
4 changes: 4 additions & 0 deletions src/Symfony/Component/Routing/Loader/XmlFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ protected function parseImport(RouteCollection $collection, \DOMElement $node, $
$subCollection->addRequirements($requirements);
$subCollection->addOptions($options);

if ($namePrefix = $node->getAttribute('name-prefix')) {
$subCollection->addNamePrefix($namePrefix);
}

$collection->addCollection($subCollection);
}

Expand Down
6 changes: 5 additions & 1 deletion src/Symfony/Component/Routing/Loader/YamlFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
class YamlFileLoader extends FileLoader
{
private static $availableKeys = array(
'resource', 'type', 'prefix', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', 'condition', 'controller',
'resource', 'type', 'prefix', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', 'condition', 'controller', 'name_prefix',
);
private $yamlParser;

Expand Down Expand Up @@ -169,6 +169,10 @@ protected function parseImport(RouteCollection $collection, array $config, $path
$subCollection->addRequirements($requirements);
$subCollection->addOptions($options);

if (isset($config['name_prefix'])) {
$subCollection->addNamePrefix($config['name_prefix']);
}

$collection->addCollection($subCollection);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<xsd:attribute name="resource" type="xsd:string" use="required" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="prefix" type="xsd:string" />
<xsd:attribute name="name-prefix" type="xsd:string" />
<xsd:attribute name="host" type="xsd:string" />
<xsd:attribute name="schemes" type="xsd:string" />
<xsd:attribute name="methods" type="xsd:string" />
Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Component/Routing/RouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,20 @@ public function addPrefix($prefix, array $defaults = array(), array $requirement
}
}

/**
* Adds a prefix to the name of all the routes within in the collection.
*/
public function addNamePrefix(string $prefix)
{
$prefixedRoutes = array();

foreach ($this->routes as $name => $route) {
$prefixedRoutes[$prefix.$name] = $route;
}

$this->routes = $prefixedRoutes;
}

/**
* Sets the host pattern on all routes.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing
http://symfony.com/schema/routing/routing-1.0.xsd">

<import resource="../controller/routing.xml" />
<import resource="../controller/routing.xml" prefix="/api" name-prefix="api_" />

</routes>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
app:
resource: ../controller/routing.yml

api:
resource: ../controller/routing.yml
name_prefix: api_
prefix: /api
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
someroute:
resource: path/to/some.yml
name_prefix: test_
not_valid_key: test_
11 changes: 11 additions & 0 deletions src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,4 +361,15 @@ public function testImportWithOverriddenController()
$loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/controller')));
$loader->load('import_override_defaults.xml');
}

public function testImportRouteWithNamePrefix()
{
$loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/import_with_name_prefix')));
$routeCollection = $loader->load('routing.xml');

$this->assertNotNull($routeCollection->get('app_blog'));
$this->assertEquals('/blog', $routeCollection->get('app_blog')->getPath());
$this->assertNotNull($routeCollection->get('api_app_blog'));
$this->assertEquals('/api/blog', $routeCollection->get('api_app_blog')->getPath());
}
}
11 changes: 11 additions & 0 deletions src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,15 @@ public function testImportWithOverriddenController()
$loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/controller')));
$loader->load('import_override_defaults.yml');
}

public function testImportRouteWithNamePrefix()
{
$loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/import_with_name_prefix')));
$routeCollection = $loader->load('routing.yml');

$this->assertNotNull($routeCollection->get('app_blog'));
$this->assertEquals('/blog', $routeCollection->get('app_blog')->getPath());
$this->assertNotNull($routeCollection->get('api_app_blog'));
$this->assertEquals('/api/blog', $routeCollection->get('api_app_blog')->getPath());
}
}
15 changes: 15 additions & 0 deletions src/Symfony/Component/Routing/Tests/RouteCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,19 @@ public function testSetMethods()
$this->assertEquals(array('PUT'), $routea->getMethods());
$this->assertEquals(array('PUT'), $routeb->getMethods());
}

public function testAddNamePrefix()
{
$collection = new RouteCollection();
$collection->add('foo', $foo = new Route('/foo'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please also add an existing api_foo route, to be sure that the prefixing does not delete this route due to overriding.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, added.

$collection->add('bar', $bar = new Route('/bar'));
$collection->add('api_foo', $apiFoo = new Route('/api/foo'));
$collection->addNamePrefix('api_');

$this->assertEquals($foo, $collection->get('api_foo'));
$this->assertEquals($bar, $collection->get('api_bar'));
$this->assertEquals($apiFoo, $collection->get('api_api_foo'));
$this->assertNull($collection->get('foo'));
$this->assertNull($collection->get('bar'));
}
}
0