You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feature #58819 [Routing] Allow aliases in #[Route] attribute (damienfern)
This PR was squashed before being merged into the 7.3 branch.
Discussion
----------
[Routing] Allow aliases in `#[Route]` attribute
| Q | A
| ------------- | ---
| Branch? | 7.3
| Bug fix? | no
| New feature? | yes <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| License | MIT
While scrolling the [Routing documentation](https://symfony.com/doc/current/routing.html#route-aliasing), I noticed that we can't configure aliases with the `#[Route]` attribute.
With this PR, we can.
```php
#[Route('/path', name: 'action_with_alias', alias: ['alias', 'completely_different_name'])]
public function action()
{
}
```
<!--
Replace this notice by a description of your feature/bugfix.
This will help reviewers and should be a good start for the documentation.
Additionally (see https://symfony.com/releases):
- Always add tests and ensure they pass.
- Bug fixes must be submitted against the lowest maintained branch where they apply
(lowest branches are regularly merged to upper ones so they get the fixes too).
- Features and deprecations must be submitted against the latest branch.
- For new features, provide some code snippets to help understand usage.
- Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry
- Never break backward compatibility (see https://symfony.com/bc).
-->
Commits
-------
d2ae097 [Routing] Allow aliases in `#[Route]` attribute
Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Attribute/Route.php
+38-15
Original file line number
Diff line number
Diff line change
@@ -22,23 +22,28 @@ class Route
22
22
privatearray$localizedPaths = [];
23
23
privatearray$methods;
24
24
privatearray$schemes;
25
+
/**
26
+
* @var (string|DeprecatedAlias)[]
27
+
*/
28
+
privatearray$aliases = [];
25
29
26
30
/**
27
-
* @param string|array<string,string>|null $path The route path (i.e. "/user/login")
28
-
* @param string|null $name The route name (i.e. "app_user_login")
29
-
* @param array<string|\Stringable> $requirements Requirements for the route attributes, @see https://symfony.com/doc/current/routing.html#parameters-validation
30
-
* @param array<string, mixed> $options Options for the route (i.e. ['prefix' => '/api'])
31
-
* @param array<string, mixed> $defaults Default values for the route attributes and query parameters
32
-
* @param string|null $host The host for which this route should be active (i.e. "localhost")
33
-
* @param string|string[] $methods The list of HTTP methods allowed by this route
34
-
* @param string|string[] $schemes The list of schemes allowed by this route (i.e. "https")
35
-
* @param string|null $condition An expression that must evaluate to true for the route to be matched, @see https://symfony.com/doc/current/routing.html#matching-expressions
36
-
* @param int|null $priority The priority of the route if multiple ones are defined for the same path
37
-
* @param string|null $locale The locale accepted by the route
38
-
* @param string|null $format The format returned by the route (i.e. "json", "xml")
39
-
* @param bool|null $utf8 Whether the route accepts UTF-8 in its parameters
40
-
* @param bool|null $stateless Whether the route is defined as stateless or stateful, @see https://symfony.com/doc/current/routing.html#stateless-routes
41
-
* @param string|null $env The env in which the route is defined (i.e. "dev", "test", "prod")
31
+
* @param string|array<string,string>|null $path The route path (i.e. "/user/login")
32
+
* @param string|null $name The route name (i.e. "app_user_login")
33
+
* @param array<string|\Stringable> $requirements Requirements for the route attributes, @see https://symfony.com/doc/current/routing.html#parameters-validation
34
+
* @param array<string, mixed> $options Options for the route (i.e. ['prefix' => '/api'])
35
+
* @param array<string, mixed> $defaults Default values for the route attributes and query parameters
36
+
* @param string|null $host The host for which this route should be active (i.e. "localhost")
37
+
* @param string|string[] $methods The list of HTTP methods allowed by this route
38
+
* @param string|string[] $schemes The list of schemes allowed by this route (i.e. "https")
39
+
* @param string|null $condition An expression that must evaluate to true for the route to be matched, @see https://symfony.com/doc/current/routing.html#matching-expressions
40
+
* @param int|null $priority The priority of the route if multiple ones are defined for the same path
41
+
* @param string|null $locale The locale accepted by the route
42
+
* @param string|null $format The format returned by the route (i.e. "json", "xml")
43
+
* @param bool|null $utf8 Whether the route accepts UTF-8 in its parameters
44
+
* @param bool|null $stateless Whether the route is defined as stateless or stateful, @see https://symfony.com/doc/current/routing.html#stateless-routes
45
+
* @param string|null $env The env in which the route is defined (i.e. "dev", "test", "prod")
46
+
* @param string|DeprecatedAlias|(string|DeprecatedAlias)[] $alias The list of aliases for this route
42
47
*/
43
48
publicfunction__construct(
44
49
string|array|null$path = null,
@@ -56,6 +61,7 @@ public function __construct(
56
61
?bool$utf8 = null,
57
62
?bool$stateless = null,
58
63
private ?string$env = null,
64
+
string|DeprecatedAlias|array$alias = [],
59
65
) {
60
66
if (\is_array($path)) {
61
67
$this->localizedPaths = $path;
@@ -64,6 +70,7 @@ public function __construct(
64
70
}
65
71
$this->setMethods($methods);
66
72
$this->setSchemes($schemes);
73
+
$this->setAliases($alias);
67
74
68
75
if (null !== $locale) {
69
76
$this->defaults['_locale'] = $locale;
@@ -201,6 +208,22 @@ public function getEnv(): ?string
0 commit comments