Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 88b89c9

Browse filesBrowse files
committed
bug #35735 [Routing] Add locale requirement for localized routes (mtarld)
This PR was merged into the 4.4 branch. Discussion ---------- [Routing] Add locale requirement for localized routes | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | License | MIT 4.4 version of #35692 If you're using localized routes, you expect to have these kind of routes available: - `/fr/accueil` - `/en/home` But nowadays, these routes are unexpectedly available: - `/en/accueil` - `/fr/home` When importing routes like that: - `prefix: "/{_locale}"` - `@Route({"en": "/home", "fr": "/accueil"}, name="home")` This PR proposes to add a strict locale requirement for localized so that the above routes won't be available. Commits ------- 50d7445 [Routing] Add locale requirement for localized routes
2 parents 12f1c7c + 50d7445 commit 88b89c9
Copy full SHA for 88b89c9

File tree

8 files changed

+22
-5
lines changed
Filter options

8 files changed

+22
-5
lines changed

‎src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Routing\Annotation\Route as RouteAnnotation;
1919
use Symfony\Component\Routing\Route;
2020
use Symfony\Component\Routing\RouteCollection;
21+
use Symfony\Component\Routing\RouteCompiler;
2122

2223
/**
2324
* AnnotationClassLoader loads routing information from a PHP class and its methods.
@@ -211,6 +212,7 @@ protected function addRoute(RouteCollection $collection, $annot, $globals, \Refl
211212
$this->configureRoute($route, $class, $method, $annot);
212213
if (0 !== $locale) {
213214
$route->setDefault('_locale', $locale);
215+
$route->setRequirement('_locale', preg_quote($locale, RouteCompiler::REGEX_DELIMITER));
214216
$route->setDefault('_canonical_route', $name);
215217
$collection->add($name.'.'.$locale, $route);
216218
} else {

‎src/Symfony/Component/Routing/Loader/Configurator/Traits/AddTrait.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Loader/Configurator/Traits/AddTrait.php
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Routing\Loader\Configurator\RouteConfigurator;
1616
use Symfony\Component\Routing\Route;
1717
use Symfony\Component\Routing\RouteCollection;
18+
use Symfony\Component\Routing\RouteCompiler;
1819

1920
trait AddTrait
2021
{
@@ -67,6 +68,7 @@ final public function add(string $name, $path): RouteConfigurator
6768
$routes->add($name.'.'.$locale, $route = $this->createRoute($path));
6869
$this->collection->add($this->name.$name.'.'.$locale, $route);
6970
$route->setDefault('_locale', $locale);
71+
$route->setRequirement('_locale', preg_quote($locale, RouteCompiler::REGEX_DELIMITER));
7072
$route->setDefault('_canonical_route', $this->name.$name);
7173
}
7274

‎src/Symfony/Component/Routing/Loader/XmlFileLoader.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Loader/XmlFileLoader.php
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Config\Util\XmlUtils;
1717
use Symfony\Component\Routing\Route;
1818
use Symfony\Component\Routing\RouteCollection;
19+
use Symfony\Component\Routing\RouteCompiler;
1920

2021
/**
2122
* XmlFileLoader loads XML routing files.
@@ -129,6 +130,7 @@ protected function parseRoute(RouteCollection $collection, \DOMElement $node, $p
129130
foreach ($paths as $locale => $p) {
130131
$defaults['_locale'] = $locale;
131132
$defaults['_canonical_route'] = $id;
133+
$requirements['_locale'] = preg_quote($locale, RouteCompiler::REGEX_DELIMITER);
132134
$route = new Route($p, $defaults, $requirements, $options, $node->getAttribute('host'), $schemes, $methods, $condition);
133135
$collection->add($id.'.'.$locale, $route);
134136
}

‎src/Symfony/Component/Routing/Loader/YamlFileLoader.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Loader/YamlFileLoader.php
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Config\Resource\FileResource;
1616
use Symfony\Component\Routing\Route;
1717
use Symfony\Component\Routing\RouteCollection;
18+
use Symfony\Component\Routing\RouteCompiler;
1819
use Symfony\Component\Yaml\Exception\ParseException;
1920
use Symfony\Component\Yaml\Parser as YamlParser;
2021
use Symfony\Component\Yaml\Yaml;
@@ -140,6 +141,7 @@ protected function parseRoute(RouteCollection $collection, $name, array $config,
140141
foreach ($config['path'] as $locale => $path) {
141142
$localizedRoute = clone $route;
142143
$localizedRoute->setDefault('_locale', $locale);
144+
$localizedRoute->setRequirement('_locale', preg_quote($locale, RouteCompiler::REGEX_DELIMITER));
143145
$localizedRoute->setDefault('_canonical_route', $name);
144146
$localizedRoute->setPath($path);
145147
$collection->add($name.'.'.$locale, $localizedRoute);

‎src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTest.php
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ public function testLocalizedPathRoutes()
125125
$this->assertCount(2, $routes);
126126
$this->assertEquals('/path', $routes->get('action.en')->getPath());
127127
$this->assertEquals('/pad', $routes->get('action.nl')->getPath());
128+
129+
$this->assertEquals('nl', $routes->get('action.nl')->getRequirement('_locale'));
130+
$this->assertEquals('en', $routes->get('action.en')->getRequirement('_locale'));
128131
}
129132

130133
public function testLocalizedPathRoutesWithExplicitPathPropety()

‎src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,11 @@ public function testRoutingI18nConfigurator()
229229

230230
$expectedCollection = new RouteCollection();
231231

232-
$expectedCollection->add('foo.en', (new Route('/glish/foo'))->setDefaults(['_locale' => 'en', '_canonical_route' => 'foo']));
233-
$expectedCollection->add('bar.en', (new Route('/glish/bar'))->setDefaults(['_locale' => 'en', '_canonical_route' => 'bar']));
234-
$expectedCollection->add('baz.en', (new Route('/baz'))->setDefaults(['_locale' => 'en', '_canonical_route' => 'baz']));
235-
$expectedCollection->add('c_foo.fr', (new Route('/ench/pub/foo'))->setDefaults(['_locale' => 'fr', '_canonical_route' => 'c_foo']));
236-
$expectedCollection->add('c_bar.fr', (new Route('/ench/pub/bar'))->setDefaults(['_locale' => 'fr', '_canonical_route' => 'c_bar']));
232+
$expectedCollection->add('foo.en', (new Route('/glish/foo'))->setDefaults(['_locale' => 'en', '_canonical_route' => 'foo'])->setRequirement('_locale', 'en'));
233+
$expectedCollection->add('bar.en', (new Route('/glish/bar'))->setDefaults(['_locale' => 'en', '_canonical_route' => 'bar'])->setRequirement('_locale', 'en'));
234+
$expectedCollection->add('baz.en', (new Route('/baz'))->setDefaults(['_locale' => 'en', '_canonical_route' => 'baz'])->setRequirement('_locale', 'en'));
235+
$expectedCollection->add('c_foo.fr', (new Route('/ench/pub/foo'))->setDefaults(['_locale' => 'fr', '_canonical_route' => 'c_foo'])->setRequirement('_locale', 'fr'));
236+
$expectedCollection->add('c_bar.fr', (new Route('/ench/pub/bar'))->setDefaults(['_locale' => 'fr', '_canonical_route' => 'c_bar'])->setRequirement('_locale', 'fr'));
237237

238238
$expectedCollection->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_dsl_sub_i18n.php')));
239239
$expectedCollection->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_dsl_i18n.php')));

‎src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ public function testLocalizedImports()
185185

186186
$this->assertEquals('/le-prefix/le-suffix', $routeCollection->get('imported.fr')->getPath());
187187
$this->assertEquals('/the-prefix/suffix', $routeCollection->get('imported.en')->getPath());
188+
189+
$this->assertEquals('fr', $routeCollection->get('imported.fr')->getRequirement('_locale'));
190+
$this->assertEquals('en', $routeCollection->get('imported.en')->getRequirement('_locale'));
188191
}
189192

190193
public function testLocalizedImportsOfNotLocalizedRoutes()

‎src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,9 @@ public function testImportingRoutesWithLocales()
321321
$this->assertCount(2, $routes);
322322
$this->assertEquals('/nl/voorbeeld', $routes->get('imported.nl')->getPath());
323323
$this->assertEquals('/en/example', $routes->get('imported.en')->getPath());
324+
325+
$this->assertEquals('nl', $routes->get('imported.nl')->getRequirement('_locale'));
326+
$this->assertEquals('en', $routes->get('imported.en')->getRequirement('_locale'));
324327
}
325328

326329
public function testImportingNonLocalizedRoutesWithLocales()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.