-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Host Locale Feature Not Applied to Static Routes #58086
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
Comments
Can you create a small example application that allows to reproduce your issue? |
I've looked at the code and think i understand what is happening here In the YamlFileLoader, the symfony/src/Symfony/Component/Routing/Loader/YamlFileLoader.php Lines 158 to 168 in c3bb47a
The following line is the key to the problem $this->addHost($routes, $config['host']); This is called on "$routes", which is not the original $collection of the method, but a temporary one, containing the route that was just added to it. See how createLocalizedRoute works in this specific case ($routes is a fresh instance, not the $collection passed in argument) symfony/src/Symfony/Component/Routing/Loader/Configurator/Traits/LocalizedRouteTrait.php Lines 55 to 58 in c3bb47a
$routes->addDefaults($defaults);
$routes->addRequirements($requirements);
$routes->addOptions($options);
$routes->setSchemes($config['schemes'] ?? []);
$routes->setMethods($config['methods'] ?? []);
$routes->setCondition($config['condition'] ?? null); Theses methods work as expected because they are all implemented to apply the data to all its children But here is the source of the troubles if (isset($config['host'])) {
$this->addHost($routes, $config['host']);
} Because addHost either apply the data to its children or ... removes a route from the collection and adds new ones. symfony/src/Symfony/Component/Routing/Loader/Configurator/Traits/HostTrait.php Lines 29 to 39 in c3bb47a
So in this specific scenario, the Now, not sure what is the best way to fix this.. maybe try to remove these "temporary collections" ? |
On a new symfony app
php bin/console debug:router Expected --------------- -------- -------- ----------------- --------------
Name Method Scheme Host Path
--------------- -------- -------- ----------------- --------------
robots_txt.en ANY ANY www.example.com /robots.txt
robots_txt.nl ANY ANY www.example.nl /robots.txt
robots_txt.de ANY ANY www.example.de /robots.txt
--------------- -------- -------- ----------------- -------------- Results ------------ -------- -------- ------ -------------
Name Method Scheme Host Path
------------ -------- -------- ------ -------------
robots_txt ANY ANY ANY /robots.txt
------------ -------- -------- ------ ------------- |
This PR was merged into the 6.4 branch. Discussion ---------- [Routing] Fix configuring a single route's hosts | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #58086 | License | MIT All loaders were affected by this issue: when you configure a route it is immediately added to the main collection **and** in the one returned by the `LocalizedRouteTrait`. Problem is: only the latter is updated when configuring hosts, which means they will be ignored. This PR merges these two collections, with a twist: after its hosts are configured, a route may have to be removed from the main collection (like `static` becoming `static.nl` and `static.en` in the tests). Commits ------- ecbaff0 [Routing] Fix configuring a single route’ hosts
Symfony version(s) affected
6.4.10
Description
The host locale feature in Symfony is intended to set the locale based on the request's host. However, this feature does not work for static routes defined in routing configuration files. While the host locale is correctly applied when using annotation routes, static routes do not respect this configuration, resulting in the inability to serve localized static content like robots.txt based on the domain or subdomain.
How to reproduce
Expected config (it not works)
Workaround (it works)
Possible Solution
No response
Additional Context
No response
The text was updated successfully, but these errors were encountered: