8000 [Routing] Fixed priority getting lost when setting localized prefix by SystematicCZ · Pull Request #52913 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Routing] Fixed priority getting lost when setting localized prefix #52913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 30, 2024
Merged
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
[Routing] Fixed priority getting lost when defining prefix array
  • Loading branch information
pritasil authored and nicolas-grekas committed Jan 30, 2024
commit ba411755aaf19f0dfb7bf06311a46b332d308e19
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,21 @@ final protected function addPrefix(RouteCollection $routes, $prefix, bool $trail
}
foreach ($routes->all() as $name => $route) {
if (null === $locale = $route->getDefault('_locale')) {
$priority = $routes->getPriority($name) ?? 0;
$routes->remove($name);
foreach ($prefix as $locale => $localePrefix) {
$localizedRoute = clone $route;
$localizedRoute->setDefault('_locale', $locale);
$localizedRoute->setRequirement('_locale', preg_quote($locale));
$localizedRoute->setDefault('_canonical_route', $name);
$localizedRoute->setPath($localePrefix.(!$trailingSlashOnRoot && '/' === $route->getPath() ? '' : $route->getPath()));
$routes->add($name.'.'.$locale, $localizedRoute);
$routes->add($name.'.'.$locale, $localizedRoute, $priority);
}
} elseif (!isset($prefix[$locale])) {
throw new \InvalidArgumentException(sprintf('Route "%s" with locale "%s" is missing a corresponding prefix in its parent collection.', $name, $locale));
} else {
$route->setPath($prefix[$locale].(!$trailingSlashOnRoot && '/' === $route->getPath() ? '' : $route->getPath()));
$routes->add($name, $route);
$routes->add($name, $route, $routes->getPriority($name) ?? 0);
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Routing/RouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -395,4 +395,9 @@ public function getAlias(string $name): ?Alias
{
return $this->aliases[$name] ?? null;
}

public function getPriority(string $name): ?int
{
return $this->priorities[$name] ?? null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures;

use Symfony\Component\Routing\Annotation\Route;

class RouteWithPriorityController
{
/**
* @Route("/important", name="important", priority=2)
*/
public function important()
{
}

8000
/**
* @Route("/also-important", name="also_important", priority=1, defaults={"_locale": "cs"})
*/
public function alsoImportant()
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
important_controllers:
resource: Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\RouteWithPriorityController
type: annotation
prefix:
cs: /cs
en: /en
26 changes: 26 additions & 0 deletions src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php