Skip to content

Commit 54c81d2

Browse files
committed
bug #27270 [Routing] Fix adding name prefix to canonical route names (ismail1432)
This PR was merged into the 4.1 branch. Discussion ---------- [Routing] Fix adding name prefix to canonical route names | Q | A | ------------- | --- | Branch? | 4.1 for bug fixes <!-- see below --> | Bug fix? | yes | New feature? |no <!-- don't forget to update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | #27244 <!-- #-prefixed issue number(s), if any --> | License | MIT This PR resolve the [bug](symfony/symfony#27244) in the [prefix imported routes name](https://symfony.com/blog/new-in-symfony-4-1-prefix-imported-route-names) feature. Reviews are always welcomed moreover as I touch a key element ( the `_canonical_route` attribute ). I need an expert in the Routing component to avoid side effect Thanks Commits ------- cb5ce8f32e fix bug when imported routes are prefixed
2 parents 381a5d0 + 553d1d6 commit 54c81d2

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

RouteCollection.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ public function addNamePrefix(string $prefix)
162162

163163
foreach ($this->routes as $name => $route) {
164164
$prefixedRoutes[$prefix.$name] = $route;
165+
if (null !== $name = $route->getDefault('_canonical_route')) {
166+
$route->setDefault('_canonical_route', $prefix.$name);
167+
}
165168
}
166169

167170
$this->routes = $prefixedRoutes;

Tests/RouteCollectionTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,4 +317,17 @@ public function testAddNamePrefix()
317317
$this->assertNull($collection->get('foo'));
318318
$this->assertNull($collection->get('bar'));
319319
}
320+
321+
public function testAddNamePrefixCanonicalRouteName()
322+
{
323+
$collection = new RouteCollection();
324+
$collection->add('foo', new Route('/foo', array('_canonical_route' => 'foo')));
325+
$collection->add('bar', new Route('/bar', array('_canonical_route' => 'bar')));
326+
$collection->add('api_foo', new Route('/api/foo', array('_canonical_route' => 'api_foo')));
327+
$collection->addNamePrefix('api_');
328+
329+
$this->assertEquals('api_foo', $collection->get('api_foo')->getDefault('_canonical_route'));
330+
$this->assertEquals('api_bar', $collection->get('api_bar')->getDefault('_canonical_route'));
331+
$this->assertEquals('api_api_foo', $collection->get('api_api_foo')->getDefault('_canonical_route'));
332+
}
320333
}

0 commit comments

Comments
 (0)