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 https://github.com/symfony/symfony/pull/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
-------
50d744589e
[Routing] Add locale requirement for localized routes
This commit is contained in:
commit
88b89c9b45
@ -18,6 +18,7 @@ use Symfony\Component\Config\Resource\FileResource;
|
|||||||
use Symfony\Component\Routing\Annotation\Route as RouteAnnotation;
|
use Symfony\Component\Routing\Annotation\Route as RouteAnnotation;
|
||||||
use Symfony\Component\Routing\Route;
|
use Symfony\Component\Routing\Route;
|
||||||
use Symfony\Component\Routing\RouteCollection;
|
use Symfony\Component\Routing\RouteCollection;
|
||||||
|
use Symfony\Component\Routing\RouteCompiler;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* AnnotationClassLoader loads routing information from a PHP class and its methods.
|
* AnnotationClassLoader loads routing information from a PHP class and its methods.
|
||||||
@ -211,6 +212,7 @@ abstract class AnnotationClassLoader implements LoaderInterface
|
|||||||
$this->configureRoute($route, $class, $method, $annot);
|
$this->configureRoute($route, $class, $method, $annot);
|
||||||
if (0 !== $locale) {
|
if (0 !== $locale) {
|
||||||
$route->setDefault('_locale', $locale);
|
$route->setDefault('_locale', $locale);
|
||||||
|
$route->setRequirement('_locale', preg_quote($locale, RouteCompiler::REGEX_DELIMITER));
|
||||||
$route->setDefault('_canonical_route', $name);
|
$route->setDefault('_canonical_route', $name);
|
||||||
$collection->add($name.'.'.$locale, $route);
|
$collection->add($name.'.'.$locale, $route);
|
||||||
} else {
|
} else {
|
||||||
|
@ -15,6 +15,7 @@ use Symfony\Component\Routing\Loader\Configurator\CollectionConfigurator;
|
|||||||
use Symfony\Component\Routing\Loader\Configurator\RouteConfigurator;
|
use Symfony\Component\Routing\Loader\Configurator\RouteConfigurator;
|
||||||
use Symfony\Component\Routing\Route;
|
use Symfony\Component\Routing\Route;
|
||||||
use Symfony\Component\Routing\RouteCollection;
|
use Symfony\Component\Routing\RouteCollection;
|
||||||
|
use Symfony\Component\Routing\RouteCompiler;
|
||||||
|
|
||||||
trait AddTrait
|
trait AddTrait
|
||||||
{
|
{
|
||||||
@ -67,6 +68,7 @@ trait AddTrait
|
|||||||
$routes->add($name.'.'.$locale, $route = $this->createRoute($path));
|
$routes->add($name.'.'.$locale, $route = $this->createRoute($path));
|
||||||
$this->collection->add($this->name.$name.'.'.$locale, $route);
|
$this->collection->add($this->name.$name.'.'.$locale, $route);
|
||||||
$route->setDefault('_locale', $locale);
|
$route->setDefault('_locale', $locale);
|
||||||
|
$route->setRequirement('_locale', preg_quote($locale, RouteCompiler::REGEX_DELIMITER));
|
||||||
$route->setDefault('_canonical_route', $this->name.$name);
|
$route->setDefault('_canonical_route', $this->name.$name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,6 +16,7 @@ use Symfony\Component\Config\Resource\FileResource;
|
|||||||
use Symfony\Component\Config\Util\XmlUtils;
|
use Symfony\Component\Config\Util\XmlUtils;
|
||||||
use Symfony\Component\Routing\Route;
|
use Symfony\Component\Routing\Route;
|
||||||
use Symfony\Component\Routing\RouteCollection;
|
use Symfony\Component\Routing\RouteCollection;
|
||||||
|
use Symfony\Component\Routing\RouteCompiler;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* XmlFileLoader loads XML routing files.
|
* XmlFileLoader loads XML routing files.
|
||||||
@ -129,6 +130,7 @@ class XmlFileLoader extends FileLoader
|
|||||||
foreach ($paths as $locale => $p) {
|
foreach ($paths as $locale => $p) {
|
||||||
$defaults['_locale'] = $locale;
|
$defaults['_locale'] = $locale;
|
||||||
$defaults['_canonical_route'] = $id;
|
$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);
|
$route = new Route($p, $defaults, $requirements, $options, $node->getAttribute('host'), $schemes, $methods, $condition);
|
||||||
$collection->add($id.'.'.$locale, $route);
|
$collection->add($id.'.'.$locale, $route);
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,7 @@ use Symfony\Component\Config\Loader\FileLoader;
|
|||||||
use Symfony\Component\Config\Resource\FileResource;
|
use Symfony\Component\Config\Resource\FileResource;
|
||||||
use Symfony\Component\Routing\Route;
|
use Symfony\Component\Routing\Route;
|
||||||
use Symfony\Component\Routing\RouteCollection;
|
use Symfony\Component\Routing\RouteCollection;
|
||||||
|
use Symfony\Component\Routing\RouteCompiler;
|
||||||
use Symfony\Component\Yaml\Exception\ParseException;
|
use Symfony\Component\Yaml\Exception\ParseException;
|
||||||
use Symfony\Component\Yaml\Parser as YamlParser;
|
use Symfony\Component\Yaml\Parser as YamlParser;
|
||||||
use Symfony\Component\Yaml\Yaml;
|
use Symfony\Component\Yaml\Yaml;
|
||||||
@ -140,6 +141,7 @@ class YamlFileLoader extends FileLoader
|
|||||||
foreach ($config['path'] as $locale => $path) {
|
foreach ($config['path'] as $locale => $path) {
|
||||||
$localizedRoute = clone $route;
|
$localizedRoute = clone $route;
|
||||||
$localizedRoute->setDefault('_locale', $locale);
|
$localizedRoute->setDefault('_locale', $locale);
|
||||||
|
$localizedRoute->setRequirement('_locale', preg_quote($locale, RouteCompiler::REGEX_DELIMITER));
|
||||||
$localizedRoute->setDefault('_canonical_route', $name);
|
$localizedRoute->setDefault('_canonical_route', $name);
|
||||||
$localizedRoute->setPath($path);
|
$localizedRoute->setPath($path);
|
||||||
$collection->add($name.'.'.$locale, $localizedRoute);
|
$collection->add($name.'.'.$locale, $localizedRoute);
|
||||||
|
@ -125,6 +125,9 @@ class AnnotationClassLoaderTest extends AbstractAnnotationLoaderTest
|
|||||||
$this->assertCount(2, $routes);
|
$this->assertCount(2, $routes);
|
||||||
$this->assertEquals('/path', $routes->get('action.en')->getPath());
|
$this->assertEquals('/path', $routes->get('action.en')->getPath());
|
||||||
$this->assertEquals('/pad', $routes->get('action.nl')->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()
|
public function testLocalizedPathRoutesWithExplicitPathPropety()
|
||||||
|
@ -229,11 +229,11 @@ class PhpFileLoaderTest extends TestCase
|
|||||||
|
|
||||||
$expectedCollection = new RouteCollection();
|
$expectedCollection = new RouteCollection();
|
||||||
|
|
||||||
$expectedCollection->add('foo.en', (new Route('/glish/foo'))->setDefaults(['_locale' => 'en', '_canonical_route' => 'foo']));
|
$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']));
|
$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']));
|
$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']));
|
$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']));
|
$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_sub_i18n.php')));
|
||||||
$expectedCollection->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_dsl_i18n.php')));
|
$expectedCollection->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_dsl_i18n.php')));
|
||||||
|
@ -185,6 +185,9 @@ class XmlFileLoaderTest extends TestCase
|
|||||||
|
|
||||||
$this->assertEquals('/le-prefix/le-suffix', $routeCollection->get('imported.fr')->getPath());
|
$this->assertEquals('/le-prefix/le-suffix', $routeCollection->get('imported.fr')->getPath());
|
||||||
$this->assertEquals('/the-prefix/suffix', $routeCollection->get('imported.en')->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()
|
public function testLocalizedImportsOfNotLocalizedRoutes()
|
||||||
|
@ -321,6 +321,9 @@ class YamlFileLoaderTest extends TestCase
|
|||||||
$this->assertCount(2, $routes);
|
$this->assertCount(2, $routes);
|
||||||
$this->assertEquals('/nl/voorbeeld', $routes->get('imported.nl')->getPath());
|
$this->assertEquals('/nl/voorbeeld', $routes->get('imported.nl')->getPath());
|
||||||
$this->assertEquals('/en/example', $routes->get('imported.en')->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()
|
public function testImportingNonLocalizedRoutesWithLocales()
|
||||||
|
Reference in New Issue
Block a user