Skip to content

[Routing] Fix configuring a single route's hosts #59446

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 9, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,14 @@ public function __construct(RouteCollection $collection, RouteCollection $route,
*/
final public function host(string|array $host): static
{
$previousRoutes = clone $this->route;
$this->addHost($this->route, $host);
foreach ($previousRoutes as $name => $route) {
if (!$this->route->get($name)) {
$this->collection->remove($name);
}
}
$this->collection->addCollection($this->route);

return $this;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Component/Routing/Loader/XmlFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ protected function parseRoute(RouteCollection $collection, \DOMElement $node, st
throw new \InvalidArgumentException(sprintf('The <route> element in file "%s" must not have both a "path" attribute and <path> child nodes.', $path));
}

$routes = $this->createLocalizedRoute($collection, $id, $paths ?: $node->getAttribute('path'));
$routes = $this->createLocalizedRoute(new RouteCollection(), $id, $paths ?: $node->getAttribute('path'));
$routes->addDefaults($defaults);
$routes->addRequirements($requirements);
$routes->addOptions($options);
Expand All @@ -146,6 +146,8 @@ protected function parseRoute(RouteCollection $collection, \DOMElement $node, st
if (null !== $hosts) {
$this->addHost($routes, $hosts);
}

$collection->addCollection($routes);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Component/Routing/Loader/YamlFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ protected function parseRoute(RouteCollection $collection, string $name, array $
$defaults['_stateless'] = $config['stateless'];
}

$routes = $this->createLocalizedRoute($collection, $name, $config['path']);
$routes = $this->createLocalizedRoute(new RouteCollection(), $name, $config['path']);
$routes->addDefaults($defaults);
$routes->addRequirements($requirements);
$routes->addOptions($options);
Expand All @@ -168,6 +168,8 @@ protected function parseRoute(RouteCollection $collection, string $name, array $
if (isset($config['host'])) {
$this->addHost($routes, $config['host']);
}

$collection->addCollection($routes);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

return function (string $format) {
$expectedRoutes = new RouteCollection();
$expectedRoutes->add('static.en', $route = new Route('/example'));
$route->setHost('www.example.com');
$route->setRequirement('_locale', 'en');
$route->setDefault('_locale', 'en');
$route->setDefault('_canonical_route', 'static');
$expectedRoutes->add('static.nl', $route = new Route('/example'));
$route->setHost('www.example.nl');
$route->setRequirement('_locale', 'nl');
$route->setDefault('_locale', 'nl');
$route->setDefault('_canonical_route', 'static');

$expectedRoutes->addResource(new FileResource(__DIR__."/route-with-hosts.$format"));

return $expectedRoutes;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Symfony\Component\Routing\Loader\Configurator;

return function (RoutingConfigurator $routes) {
$routes->add('static', '/example')->host([
'nl' => 'www.example.nl',
'en' => 'www.example.com',
]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing
https://symfony.com/schema/routing/routing-1.0.xsd">
<route id="static" path="/example">
<host locale="nl">www.example.nl</host>
<host locale="en">www.example.com</host>
</route>
</routes>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
static:
path: /example
host:
nl: www.example.nl
en: www.example.com
10 changes: 10 additions & 0 deletions src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,16 @@ public function testImportingRoutesWithSingleHostInImporter()
$this->assertEquals($expectedRoutes('php'), $routes);
}

public function testAddingRouteWithHosts()
{
$loader = new PhpFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host']));
$routes = $loader->load('route-with-hosts.php');

$expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/route-with-hosts-expected-collection.php';

$this->assertEquals($expectedRoutes('php'), $routes);
}

public function testImportingAliases()
{
$loader = new PhpFileLoader(new FileLocator([__DIR__.'/../Fixtures/alias']));
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,16 @@ public function testImportingRoutesWithSingleHostsInImporter()
$this->assertEquals($expectedRoutes('xml'), $routes);
}

public function testAddingRouteWithHosts()
{
$loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host']));
$routes = $loader->load('route-with-hosts.xml');

$expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/route-with-hosts-expected-collection.php';

$this->assertEquals($expectedRoutes('xml'), $routes);
}

public function testWhenEnv()
{
$loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures']), 'some-env');
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,16 @@ public function testImportingRoutesWithSingleHostInImporter()
$this->assertEquals($expectedRoutes('yml'), $routes);
}

public function testAddingRouteWithHosts()
{
$loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host']));
$routes = $loader->load('route-with-hosts.yml');

$expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/route-with-hosts-expected-collection.php';

$this->assertEquals($expectedRoutes('yml'), $routes);
}

public function testWhenEnv()
{
$loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures']), 'some-env');
Expand Down