8000 Ensure routes are not overwritten if have the same name · symfony/symfony@841d5ee · GitHub
[go: up one dir, main page]

Skip to content

Commit 841d5ee

Browse files
committed
Ensure routes are not overwritten if have the same name
Add the `addNamePrefix` method to the PHP trait
1 parent e0a8690 commit 841d5ee

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

src/Symfony/Component/Routing/Loader/Configurator/Traits/RouteTrait.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,18 @@ final public function controller($controller)
124124

125125
return $this;
126126
}
127+
128+
/**
129+
* Add a prefix to the name of all the routes within the collection.
130+
*
131+
* @param string $prefix
132+
*
133+
* @return $this
134+
*/
135+
final public function addNamePrefix(string $prefix)
136+
{
137+
$this->route->addNamePrefix($prefix);
138+
139+
return $this;
140+
}
127141
}

src/Symfony/Component/Routing/RouteCollection.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,13 @@ public function addPrefix($prefix, array $defaults = array(), array $requirement
160160
*/
161161
public function addNamePrefix(string $prefix)
162162
{
163+
$prefixedRoutes = array();
164+
163165
foreach ($this->routes as $name => $route) {
164-
$this->routes[$prefix.$name] = $route;
165-
unset($this->routes[$name]);
166+
$prefixedRoutes[$prefix.$name] = $route;
166167
}
168+
169+
$this->routes = $prefixedRoutes;
167170
}
168171

169172
/**

src/Symfony/Component/Routing/Tests/RouteCollectionTest.php

Lines changed: 2 additions & 0 deletions
310
Original file line numberDiff line numberDiff line change
@@ -308,10 +308,12 @@ public function testAddNamePrefix()
308308
$collection = new RouteCollection();
309309
$collection->add('foo', $foo = new Route('/foo'));
310
$collection->add('bar', $bar = new Route('/bar'));
311+
$collection->add('api_foo', $apiFoo = new Route('/api/foo'));
311312
$collection->addNamePrefix('api_');
312313

313314
$this->assertEquals($foo, $collection->get('api_foo'));
314315
$this->assertEquals($bar, $collection->get('api_bar'));
316+
$this->assertEquals($apiFoo, $collection->get('api_api_foo'));
315317
$this->assertNull($collection->get('foo'));
316318
$this->assertNull($collection->get('bar'));
317319
}

0 commit comments

Comments
 (0)
0