diff --git a/src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php b/src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php index 6390a79f5811..ce61032ac341 100644 --- a/src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php +++ b/src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php @@ -18,6 +18,7 @@ use Symfony\Component\Routing\Annotation\Route as RouteAnnotation; use Symfony\Component\Routing\Route; use Symfony\Component\Routing\RouteCollection; +use Symfony\Component\Routing\RouteCompiler; /** * AnnotationClassLoader loads routing information from a PHP class and its methods. @@ -211,6 +212,7 @@ protected function addRoute(RouteCollection $collection, $annot, $globals, \Refl $this->configureRoute($route, $class, $method, $annot); if (0 !== $locale) { $route->setDefault('_locale', $locale); + $route->setRequirement('_locale', preg_quote($locale, RouteCompiler::REGEX_DELIMITER)); $route->setDefault('_canonical_route', $name); $collection->add($name.'.'.$locale, $route); } else { diff --git a/src/Symfony/Component/Routing/Loader/Configurator/Traits/AddTrait.php b/src/Symfony/Component/Routing/Loader/Configurator/Traits/AddTrait.php index 085fde4bc9f4..84899aa2e27f 100644 --- a/src/Symfony/Component/Routing/Loader/Configurator/Traits/AddTrait.php +++ b/src/Symfony/Component/Routing/Loader/Configurator/Traits/AddTrait.php @@ -15,6 +15,7 @@ use Symfony\Component\Routing\Loader\Configurator\RouteConfigurator; use Symfony\Component\Routing\Route; use Symfony\Component\Routing\RouteCollection; +use Symfony\Component\Routing\RouteCompiler; trait AddTrait { @@ -67,6 +68,7 @@ final public function add(string $name, $path): RouteConfigurator $routes->add($name.'.'.$locale, $route = $this->createRoute($path)); $this->collection->add($this->name.$name.'.'.$locale, $route); $route->setDefault('_locale', $locale); + $route->setRequirement('_locale', preg_quote($locale, RouteCompiler::REGEX_DELIMITER)); $route->setDefault('_canonical_route', $this->name.$name); } diff --git a/src/Symfony/Component/Routing/Loader/XmlFileLoader.php b/src/Symfony/Component/Routing/Loader/XmlFileLoader.php index dc208f28e16c..da8f34b293c1 100644 --- a/src/Symfony/Component/Routing/Loader/XmlFileLoader.php +++ b/src/Symfony/Component/Routing/Loader/XmlFileLoader.php @@ -16,6 +16,7 @@ use Symfony\Component\Config\Util\XmlUtils; use Symfony\Component\Routing\Route; use Symfony\Component\Routing\RouteCollection; +use Symfony\Component\Routing\RouteCompiler; /** * XmlFileLoader loads XML routing files. @@ -129,6 +130,7 @@ protected function parseRoute(RouteCollection $collection, \DOMElement $node, $p foreach ($paths as $locale => $p) { $defaults['_locale'] = $locale; $defaults['_canonical_route'] = $id; + $requirements['_locale'] = preg_quote($locale, RouteCompiler::REGEX_DELIMITER); $route = new Route($p, $defaults, $requirements, $options, $node->getAttribute('host'), $schemes, $methods, $condition); $collection->add($id.'.'.$locale, $route); } diff --git a/src/Symfony/Component/Routing/Loader/YamlFileLoader.php b/src/Symfony/Component/Routing/Loader/YamlFileLoader.php index 0de36c93b2e5..868da9bd30b4 100644 --- a/src/Symfony/Component/Routing/Loader/YamlFileLoader.php +++ b/src/Symfony/Component/Routing/Loader/YamlFileLoader.php @@ -15,6 +15,7 @@ use Symfony\Component\Config\Resource\FileResource; use Symfony\Component\Routing\Route; use Symfony\Component\Routing\RouteCollection; +use Symfony\Component\Routing\RouteCompiler; use Symfony\Component\Yaml\Exception\ParseException; use Symfony\Component\Yaml\Parser as YamlParser; use Symfony\Component\Yaml\Yaml; @@ -140,6 +141,7 @@ protected function parseRoute(RouteCollection $collection, $name, array $config, foreach ($config['path'] as $locale => $path) { $localizedRoute = clone $route; $localizedRoute->setDefault('_locale', $locale); + $localizedRoute->setRequirement('_locale', preg_quote($locale, RouteCompiler::REGEX_DELIMITER)); $localizedRoute->setDefault('_canonical_route', $name); $localizedRoute->setPath($path); $collection->add($name.'.'.$locale, $localizedRoute); diff --git a/src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTest.php b/src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTest.php index 441900775114..c8794e10b674 100644 --- a/src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTest.php +++ b/src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTest.php @@ -125,6 +125,9 @@ public function testLocalizedPathRoutes() $this->assertCount(2, $routes); $this->assertEquals('/path', $routes->get('action.en')->getPath()); $this->assertEquals('/pad', $routes->get('action.nl')->getPath()); + + $this->assertEquals('nl', $routes->get('action.nl')->getRequirement('_locale')); + $this->assertEquals('en', $routes->get('action.en')->getRequirement('_locale')); } public function testLocalizedPathRoutesWithExplicitPathPropety() diff --git a/src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php b/src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php index 71f9df15470a..789848c66021 100644 --- a/src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php +++ b/src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php @@ -229,11 +229,11 @@ public function testRoutingI18nConfigurator() $expectedCollection = new RouteCollection(); - $expectedCollection->add('foo.en', (new Route('/glish/foo'))->setDefaults(['_locale' => 'en', '_canonical_route' => 'foo'])); - $expectedCollection->add('bar.en', (new Route('/glish/bar'))->setDefaults(['_locale' => 'en', '_canonical_route' => 'bar'])); - $expectedCollection->add('baz.en', (new Route('/baz'))->setDefaults(['_locale' => 'en', '_canonical_route' => 'baz'])); - $expectedCollection->add('c_foo.fr', (new Route('/ench/pub/foo'))->setDefaults(['_locale' => 'fr', '_canonical_route' => 'c_foo'])); - $expectedCollection->add('c_bar.fr', (new Route('/ench/pub/bar'))->setDefaults(['_locale' => 'fr', '_canonical_route' => 'c_bar'])); + $expectedCollection->add('foo.en', (new Route('/glish/foo'))->setDefaults(['_locale' => 'en', '_canonical_route' => 'foo'])->setRequirement('_locale', 'en')); + $expectedCollection->add('bar.en', (new Route('/glish/bar'))->setDefaults(['_locale' => 'en', '_canonical_route' => 'bar'])->setRequirement('_locale', 'en')); + $expectedCollection->add('baz.en', (new Route('/baz'))->setDefaults(['_locale' => 'en', '_canonical_route' => 'baz'])->setRequirement('_locale', 'en')); + $expectedCollection->add('c_foo.fr', (new Route('/ench/pub/foo'))->setDefaults(['_locale' => 'fr', '_canonical_route' => 'c_foo'])->setRequirement('_locale', 'fr')); + $expectedCollection->add('c_bar.fr', (new Route('/ench/pub/bar'))->setDefaults(['_locale' => 'fr', '_canonical_route' => 'c_bar'])->setRequirement('_locale', 'fr')); $expectedCollection->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_dsl_sub_i18n.php'))); $expectedCollection->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_dsl_i18n.php'))); diff --git a/src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php b/src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php index e27149f93125..66d54fc985c4 100644 --- a/src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php +++ b/src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php @@ -185,6 +185,9 @@ public function testLocalizedImports() $this->assertEquals('/le-prefix/le-suffix', $routeCollection->get('imported.fr')->getPath()); $this->assertEquals('/the-prefix/suffix', $routeCollection->get('imported.en')->getPath()); + + $this->assertEquals('fr', $routeCollection->get('imported.fr')->getRequirement('_locale')); + $this->assertEquals('en', $routeCollection->get('imported.en')->getRequirement('_locale')); } public function testLocalizedImportsOfNotLocalizedRoutes() diff --git a/src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php b/src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php index caad0aa978ea..52c21c287fcf 100644 --- a/src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php +++ b/src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php @@ -321,6 +321,9 @@ public function testImportingRoutesWithLocales() $this->assertCount(2, $routes); $this->assertEquals('/nl/voorbeeld', $routes->get('imported.nl')->getPath()); $this->assertEquals('/en/example', $routes->get('imported.en')->getPath()); + + $this->assertEquals('nl', $routes->get('imported.nl')->getRequirement('_locale')); + $this->assertEquals('en', $routes->get('imported.en')->getRequirement('_locale')); } public function testImportingNonLocalizedRoutesWithLocales()