diff --git a/src/Symfony/Component/Routing/Loader/AttributeClassLoader.php b/src/Symfony/Component/Routing/Loader/AttributeClassLoader.php index 254582bf35584..69ccaf7f26ea1 100644 --- a/src/Symfony/Component/Routing/Loader/AttributeClassLoader.php +++ b/src/Symfony/Component/Routing/Loader/AttributeClassLoader.php @@ -242,17 +242,21 @@ protected function addRoute(RouteCollection $collection, object $attr, array $gl $collection->add($name, $route, $priority); } foreach ($attr->getAliases() as $aliasAttribute) { + $original = $name; + if (0 !== $locale) { + $original .= '.'.$locale; + } + if ($aliasAttribute instanceof DeprecatedAlias) { - $alias = $collection->addAlias($aliasAttribute->getAliasName(), $name); + $alias = $collection->addAlias($aliasAttribute->getAliasName(), $original); $alias->setDeprecated( $aliasAttribute->getPackage(), $aliasAttribute->getVersion(), $aliasAttribute->getMessage() ); - continue; + } else { + $collection->addAlias($aliasAttribute, $original); } - - $collection->addAlias($aliasAttribute, $name); } } } diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/AttributeFixtures/AliasLocalizedRouteController.php b/src/Symfony/Component/Routing/Tests/Fixtures/AttributeFixtures/AliasLocalizedRouteController.php new file mode 100644 index 0000000000000..6fbd4eb127218 --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/AttributeFixtures/AliasLocalizedRouteController.php @@ -0,0 +1,22 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures; + +use Symfony\Component\Routing\Attribute\Route; + +class AliasLocalizedRouteController +{ + #[Route(['nl_NL' => '/nl/localized', 'fr_FR' => '/fr/localized'], name: 'localized_route', alias: ['localized_alias'])] + public function localized() + { + } +} diff --git a/src/Symfony/Component/Routing/Tests/Loader/AttributeClassLoaderTest.php b/src/Symfony/Component/Routing/Tests/Loader/AttributeClassLoaderTest.php index 50a10a16cac2f..f6e8f30ceac13 100644 --- a/src/Symfony/Component/Routing/Tests/Loader/AttributeClassLoaderTest.php +++ b/src/Symfony/Component/Routing/Tests/Loader/AttributeClassLoaderTest.php @@ -18,6 +18,7 @@ use Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\ActionPathController; use Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\AliasClassController; use Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\AliasInvokableController; +use Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\AliasLocalizedRouteController; use Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\AliasRouteController; use Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\BazClass; use Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\DefaultValueController; @@ -381,6 +382,19 @@ public function testAliasesOnMethod() $this->assertEquals(new Alias('action_with_alias'), $routes->getAlias('completely_different_name')); } + public function testLocalizedRouteWithAliases() + { + $routes = $this->loader->load(AliasLocalizedRouteController::class); + $this->assertCount(2, $routes); + + $routeNl = $routes->get('localized_route.nl_NL'); + $routeFr = $routes->get('localized_route.fr_FR'); + + $this->assertSame('/nl/localized', $routeNl->getPath()); + $this->assertSame('/fr/localized', $routeFr->getPath()); + $this->assertEquals(new Alias('localized_route.nl_NL'), $routes->getAlias('localized_alias')); + } + public function testThrowsWithAliasesOnClass() { $this->expectException(\InvalidArgumentException::class);