Skip to content

Commit a4c836c

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 a4c836c

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

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

+14
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

+5-2
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

+3-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public function testAddDefaultsAndRequirementsAndOptions()
118118
$collection->add('foo', new Route('/{placeholder}'));
119119
$collection1 = new RouteCollection();
120120
$collection1->add('bar', new Route('/{placeholder}',
121-
array('_controller' => 'fixed', 'placeholder' => 'default'), array('placeholder' => '.+'), array('option' => 'value'))
121+
array('_controller' => 'fixed', 'placeholder' => 'default'), array('placeholder' => '.+'), array('option' => 'value'))
122122
);
123123
$collection->addCollection($collection1);
124124

@@ -308,10 +308,12 @@ public function testAddNamePrefix()
308308
$collection = new RouteCollection();
309309
$collection->add('foo', $foo = new Route('/foo'));
310310
$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)