8000 [WIP] [Routing] set aliases in Route attribute · symfony/symfony@354b65b · GitHub
[go: up one dir, main page]

Skip to content

Commit 354b65b

Browse files
committed
[WIP] [Routing] set aliases in Route attribute
1 parent 3ba7a48 commit 354b65b

File tree

5 files changed

+43
-3
lines changed

5 files changed

+43
-3
lines changed

src/Symfony/Component/Routing/Attribute/Route.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public function __construct(
4646
private array $requirements = [],
4747
private array $options = [],
4848
private array $defaults = [],
49+
private array $alias = [],
4950
private ?string $host = null,
5051
array|string $methods = [],
5152
array|string $schemes = [],
@@ -64,7 +65,7 @@ public function __construct(
6465
}
6566
$this->setMethods($methods);
6667
$this->setSchemes($schemes);
67-
68+
$this->setAliases($alias);
6869
if (null !== $locale) {
6970
$this->defaults['_locale'] = $locale;
7071
}
@@ -201,6 +202,16 @@ public function getEnv(): ?string
201202
{
202203
return $this->env;
203204
}
205+
206+
public function getAliases(): array
207+
{
208+
return $this->alias;
209+
}
210+
211+
public function setAliases(array $alias): void
212+
{
213+
$this->alias = $alias;
214+
}
204215
}
205216

206217
if (!class_exists(\Symfony\Component\Routing\Annotation\Route::class, false)) {

src/Symfony/Component/Routing/CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ CHANGELOG
44
7.2
55
---
66

7-
* Add the `Requirement::UID_RFC9562` constant to validate UUIDs in the RFC 9562 format
8-
* Deprecate the `AttributeClassLoader::$routeAnnotationClass` property
7+
* Add aliases in the Route attribute
8+
* Add the `Requirement::UID_RFC9562` constant to validate UUIDs in the RFC 9562 format
9+
* Deprecate the `AttributeClassLoader::$routeAnnotationClass` property
910

1011
7.1
1112
---

src/Symfony/Component/Routing/Loader/AttributeClassLoader.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,11 @@ protected function addRoute(RouteCollection $collection, object $attr, array $gl
230230
} else {
231231
$collection->add($name, $route, $priority);
232232
}
233+
if (0 !== count($attr->getAliases())) {
234+
foreach ($attr->getAliases() as $alias) {
235+
$collection->addAlias($alias, $name);
236+
}
237+
}
233238
}
234239
}
235240

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures;
4+
5+
use Symfony\Component\Routing\Attribute\Route;
6+
7+
class AliasRouteController
8+
{
9+
#[Route('/path', name: 'action_with_alias', alias: ['alias', 'completely_different_name'])]
10+
public function action()
11+
{
12+
}
13+
}

src/Symfony/Component/Routing/Tests/Loader/AttributeClassLoaderTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Routing\Exception\LogicException;
1717
use Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\AbstractClassController;
1818
use Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\ActionPathController;
19+
use Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\AliasRouteController;
1920
use Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\BazClass;
2021
use Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\DefaultValueController;
2122
use Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\EncodingClass;
@@ -364,4 +365,13 @@ public function testDefaultRouteName()
364365

365366
$this->assertSame('symfony_component_routing_tests_fixtures_attributefixtures_encodingclass_routeàction', $defaultName);
366367
}
368+
369+
public function testAliases(): void {
370+
$routes = $this->loader->load(AliasRouteController::class);
371+
$route = $routes->get('action_with_alias');
372+
$this->assertCount(1, $routes);
373+
$this->assertEquals('/path', $route->getPath());
374+
$this->assertEquals(new Alias('action_with_alias'), $routes->getAlias('alias'));
375+
$this->assertEquals(new Alias('action_with_alias'), $routes->getAlias('completely_different_name'));
376+
}
367377
}

0 commit comments

Comments
 (0)
0