Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Symfony/Component/Routing/Attribute/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class Route
* @param bool|null $stateless Whether the route is defined as stateless or stateful, @see https://symfony.com/doc/current/routing.html#stateless-routes
* @param string|null $env The env in which the route is defined (i.e. "dev", "test", "prod")
* @param string|DeprecatedAlias|(string|DeprecatedAlias)[] $alias The list of aliases for this route
* @param bool|null $override Whether the route should override existing routes with the same name
*/
public function __construct(
string|array|null $path = null,
Expand All @@ -62,6 +63,7 @@ public function __construct(
?bool $stateless = null,
private ?string $env = null,
string|DeprecatedAlias|array $alias = [],
private ?bool $override = null,
) {
if (\is_array($path)) {
$this->localizedPaths = $path;
Expand Down Expand Up @@ -224,6 +226,16 @@ public function setAliases(string|DeprecatedAlias|array $aliases): void
{
$this->aliases = \is_array($aliases) ? $aliases : [$aliases];
}

public function setOverride(?bool $override): void
{
$this->override = $override;
}

public function getOverride(): ?bool
{
return $this->override;
}
}

if (!class_exists(\Symfony\Component\Routing\Annotation\Route::class, false)) {
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Routing/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* Allow aliases and deprecations in `#[Route]` attribute
* Add the `Requirement::MONGODB_ID` constant to validate MongoDB ObjectIDs in hexadecimal format
* Add `override` parameter in `#[Route]` attribute

7.2
---
Expand Down
5 changes: 3 additions & 2 deletions src/Symfony/Component/Routing/Loader/AttributeClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ protected function addRoute(RouteCollection $collection, object $attr, array $gl
$host = $attr->getHost() ?? $globals['host'];
$condition = $attr->getCondition() ?? $globals['condition'];
$priority = $attr->getPriority() ?? $globals['priority'];
$override = $attr->getOverride() ?? $globals['override'];

$path = $attr->getLocalizedPaths() ?: $attr->getPath();
$prefix = $globals['localized_paths'] ?: $globals['path'];
Expand Down Expand Up @@ -237,9 +238,9 @@ protected function addRoute(RouteCollection $collection, object $attr, array $gl
$route->setDefault('_locale', $locale);
$route->setRequirement('_locale', preg_quote($locale));
$route->setDefault('_canonical_route', $name);
$collection->add($name.'.'.$locale, $route, $priority);
$collection->add($name.'.'.$locale, $route, $priority, $override);
} else {
$collection->add($name, $route, $priority);
$collection->add($name, $route, $priority, $override);
}
foreach ($attr->getAliases() as $aliasAttribute) {
if ($aliasAttribute instanceof DeprecatedAlias) {
Expand Down
6 changes: 4 additions & 2 deletions src/Symfony/Component/Routing/RouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,11 @@ public function count(): int
return \count($this->routes);
}

public function add(string $name, Route $route, int $priority = 0): void
public function add(string $name, Route $route, int $priority = 0, bool $override = true): void
{
unset($this->routes[$name], $this->priorities[$name], $this->aliases[$name]);
if ($override) {
unset($this->routes[$name], $this->priorities[$name], $this->aliases[$name]);
}

$this->routes[$name] = $route;

Expand Down
Loading