8000 Ensure prefix & host are working well together · symfony/symfony@3d8ccf8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3d8ccf8

Browse files
author
Olivier Dolbeau
committed
Ensure prefix & host are working well together
1 parent 90045b0 commit 3d8ccf8

File tree

5 files changed

+68
-15
lines changed

5 files changed

+68
-15
lines changed

src/Symfony/Component/Routing/Loader/Configurator/Traits/LocalizedRouteTrait.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ trait LocalizedRouteTrait
2626
* Creates one or many routes.
2727
*
2828
* @param string|array $path the path, or the localized paths of the route
29-
*
30-
* @return Route|RouteCollection
3129
*/
32-
final protected function createLocalizedRoute(RouteCollection $collection, string $name, $path, string $namePrefix = '', array $prefixes = null)
30+
final protected function createLocalizedRoute(RouteCollection $collection, string $name, $path, string $namePrefix = '', array $prefixes = null): RouteCollection
3331
{
3432
$paths = [];
3533

34+
$routes = new RouteCollection();
35+
3636
if (\is_array($path)) {
3737
if (null === $prefixes) {
3838
$paths = $path;
@@ -52,13 +52,12 @@ final protected function createLocalizedRoute(RouteCollection $collection, strin
5252
$paths[$locale] = $prefix.$path;
5353
}
5454
} else {
55-
$collection->add($namePrefix.$name, $route = $this->createRoute($path));
55+
$routes->add($namePrefix.$name, $route = $this->createRoute($path));
56+
$collection->add($namePrefix.$name, $route);
5657

57-
return $route;
58+
return $routes;
5859
}
5960

60-
$routes = new RouteCollection();
61-
6261
foreach ($paths as $locale => $path) {
6362
$routes->add($name.'.'.$locale, $route = $this->createRoute($path));
6463
$collection->add($namePrefix.$name.'.'.$locale, $route);

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,14 @@ protected function parseRoute(RouteCollection $collection, string $name, array $
139139
$defaults['_stateless'] = $config['stateless'];
140140
}
141141

142-
$route = $this->createLocalizedRoute($collection, $name, $config['path']);
143-
$route->addDefaults($defaults);
144-
$route->addRequirements($requirements);
145-
$route->addOptions($options);
146-
$route->setHost($config['host'] ?? '');
147-
$route->setSchemes($config['schemes'] ?? []);
148-
$route->setMethods($config['methods'] ?? []);
149-
$route->setCondition($config['condition'] ?? null);
142+
$routes = $this->createLocalizedRoute($collection, $name, $config['path']);
143+
$routes->addDefaults($defaults);
144+
$routes->addRequirements($requirements);
145+
$routes->addOptions($options);
146+
$routes->setSchemes($config['schemes'] ?? []);
147+
$routes->setMethods($config['methods'] ?? []);
148+
$routes->setCondition($config['condition'] ?? null);
149+
$this->addHost($routes< 8000 /span>, $config['host'] ?? null);
150150
}
151151

152152
/**
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
imported:
3+
controller: ImportedController::someAction
4+
path:
5+
nl: /voorbeeld
6+
en: /example
7+
host:
8+
nl: www.custom.nl
9+
en: www.custom.com
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
i_need:
3+
resource: ./imported-with-locale-and-host.yml
4+
prefix:
5+
nl: /nl
6+
en: /en
7+
host:
8+
nl: www.example.nl
9+
en: www.example.com

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,42 @@ public function testImportingRoutesWithHosts()
419419
$this->assertEquals('www.example.com', $route->getHost());
420420
}
421421

422+
public function testImportingRoutesWithHostsFromDefinition()
423+
{
424+
$loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/localized']));
425+
$routes = $loader->load('imported-with-locale-and-host.yml');
426+
427+
$this->assertCount(2, $routes);
428+
429+
$route = $routes->get('imported.en');
430+
$this->assertEquals('/example', $route->getPath());
431+
$this->assertEquals('www.custom.com', $route->getHost());
432+
$this->assertEquals('en', $route->getRequirement('_locale'));
433+
434+
$route = $routes->get('imported.nl');
435+
$this->assertEquals('/voorbeeld', $route->getPath());
436+
$this->assertEquals('www.custom.nl', $route->getHost());
437+
$this->assertEquals('nl', $route->getRequirement('_locale'));
438+
}
439+
440+
public function testImportingRoutesWithLocalesAndHost()
441+
{
442+
$loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/localized']));
443+
$routes = $loader->load('importer-with-locale-and-host.yml');
444+
445+
$this->assertCount(2, $routes);
446+
447+
$route = $routes->get('imported.en');
448+
$this->assertEquals('/en/example', $route->getPath());
449+
$this->assertEquals('www.example.com', $route->getHost());
450+
$this->assertEquals('en', $route->getRequirement('_locale'));
451+
452+
$route = $routes->get('imported.nl');
453+
$this->assertEquals('/nl/voorbeeld', $route->getPath());
454+
$this->assertEquals('www.example.nl', $route->getHost());
455+
$this->assertEquals('nl', $route->getRequirement('_locale'));
456+
}
457+
422458
public function testImportRouteWithNoTrailingSlash()
423459
{
424460
$loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/import_with_no_trailing_slash']));

0 commit comments

Comments
 (0)
0