Skip to content

Commit

Permalink
bug #27270 [Routing] Fix adding name prefix to canonical route names …
Browse files Browse the repository at this point in the history
…(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](#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
-------

cb5ce8f fix bug when imported routes are prefixed
  • Loading branch information
fabpot committed May 17, 2018
2 parents 160c97e + cb5ce8f commit 20cc065
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Symfony/Component/Routing/RouteCollection.php
Expand Up @@ -162,6 +162,9 @@ public function addNamePrefix(string $prefix)

foreach ($this->routes as $name => $route) {
$prefixedRoutes[$prefix.$name] = $route;
if (null !== $name = $route->getDefault('_canonical_route')) {
$route->setDefault('_canonical_route', $prefix.$name);
}
}

$this->routes = $prefixedRoutes;
Expand Down
13 changes: 13 additions & 0 deletions src/Symfony/Component/Routing/Tests/RouteCollectionTest.php
Expand Up @@ -317,4 +317,17 @@ public function testAddNamePrefix()
$this->assertNull($collection->get('foo'));
$this->assertNull($collection->get('bar'));
}

public function testAddNamePrefixCanonicalRouteName()
{
$collection = new RouteCollection();
$collection->add('foo', new Route('/foo', array('_canonical_route' => 'foo')));
$collection->add('bar', new Route('/bar', array('_canonical_route' => 'bar')));
$collection->add('api_foo', new Route('/api/foo', array('_canonical_route' => 'api_foo')));
$collection->addNamePrefix('api_');

$this->assertEquals('api_foo', $collection->get('api_foo')->getDefault('_canonical_route'));
$this->assertEquals('api_bar', $collection->get('api_bar')->getDefault('_canonical_route'));
$this->assertEquals('api_api_foo', $collection->get('api_api_foo')->getDefault('_canonical_route'));
}
}

0 comments on commit 20cc065

Please sign in to comment.