Skip to content
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

Locale switching when there are 2 or more different hosts #1792

Open
vityachis opened this issue Dec 16, 2024 · 1 comment
Open

Locale switching when there are 2 or more different hosts #1792

vityachis opened this issue Dec 16, 2024 · 1 comment

Comments

@vityachis
Copy link

Environment

Sonata packages

show

$ composer show --latest 'sonata-project/*'
Direct dependencies required in composer.json:
sonata-project/admin-bundle              4.34.0 4.34.0 The missing Symfony Admin Generator
sonata-project/block-bundle              5.1.1  5.1.1  Symfony SonataBlockBundle
sonata-project/cache                     2.2.0  2.2.0  Cache library
Package sonata-project/cache is abandoned, you should avoid using it. No replacement was suggested.
sonata-project/cache-bundle              3.4.0  3.4.0  This bundle provides caching services
Package sonata-project/cache-bundle is abandoned, you should avoid using it. No replacement was suggested.
sonata-project/classification-bundle     4.9.1  4.9.1  Symfony SonataClassificationBundle
sonata-project/datagrid-bundle           3.5.0  3.5.0  Symfony SonataDatagridBundle
Package sonata-project/datagrid-bundle is abandoned, you should avoid using it. No replacement was suggested.
sonata-project/doctrine-extensions       2.4.1  2.4.1  Doctrine2 behavioral extensions
sonata-project/doctrine-orm-admin-bundle 4.17.1 4.17.1 Integrate Doctrine ORM into the SonataAdminBundle
sonata-project/entity-audit-bundle       1.18.0 1.18.0 Audit for Doctrine Entities
sonata-project/exporter                  3.3.0  3.3.0  Lightweight Exporter library
sonata-project/formatter-bundle          5.4.1  5.4.1  Symfony SonataFormatterBundle
sonata-project/intl-bundle               3.2.0  3.2.0  Symfony SonataIntlBundle
sonata-project/media-bundle              4.15.0 4.15.0 Symfony SonataMediaBundle
sonata-project/page-bundle               4.8.0  4.8.0  This bundle provides a Site and Page management through container and block services
sonata-project/seo-bundle                3.8.0  3.8.0  Symfony SonataSeoBundle
sonata-project/translation-bundle        3.3.0  3.3.0  SonataTranslationBundle
sonata-project/user-bundle               5.14.0 5.14.0 Symfony SonataUserBundle

Transitive dependencies not required in composer.json:
sonata-project/form-extensions           2.4.0  2.4.0  Symfony form extensions
sonata-project/twig-extensions           2.5.0  2.5.0  Sonata twig extensions

Symfony packages

show

$ composer show --latest 'symfony/*'
Direct dependencies required in composer.json:
symfony/asset                      6.4.13 7.2.0  Manages URL generation and versioning of web assets such as CSS stylesheets, JavaScript files and image files
symfony/asset-mapper               6.4.16 7.2.0  Maps directories of assets & makes them available in a public directory with versioned filenames.
symfony/browser-kit                6.4.13 7.2.0  Simulates the behavior of a web browser, allowing you to make requests, click on links and submit forms programmatically
symfony/console                    6.4.15 7.2.1  Eases the creation of beautiful and testable command line interfaces
symfony/css-selector               6.4.13 7.2.0  Converts CSS selectors to XPath expressions
symfony/debug-bundle               6.4.13 7.2.0  Provides a tight integration of the Symfony VarDumper component and the ServerLogCommand from MonologBridge into the Symfony full-st...
symfony/doctrine-messenger         6.4.13 7.2.0  Symfony Doctrine Messenger Bridge
symfony/dotenv                     6.4.16 7.2.0  Registers environment variables from a .env file
symfony/expression-language        6.4.13 7.2.0  Provides an engine that can compile and evaluate expressions
symfony/flex                       2.4.7  2.4.7  Composer plugin for Symfony
symfony/form                       6.4.13 7.2.0  Allows to easily create, process and reuse HTML forms
symfony/framework-bundle           6.4.13 7.2.1  Provides a tight integration between Symfony components and the Symfony full-stack framework
symfony/http-client                6.4.16 7.2.1  Provides powerful methods to fetch HTTP resources synchronously or asynchronously
symfony/intl                       6.4.15 7.2.0  Provides access to the localization data of the ICU library
symfony/mailer                     6.4.13 7.2.0  Helps sending emails
symfony/maker-bundle               1.61.0 1.61.0 Symfony Maker helps you create empty commands, controllers, form classes, tests and more so you can forget about writing boilerplate...
symfony/mime                       6.4.13 7.2.1  Allows manipulating MIME messages
symfony/monolog-bundle             3.10.0 3.10.0 Symfony MonologBundle
symfony/notifier                   6.4.13 7.2.0  Sends notifications via one or more channels (email, SMS, ...)
symfony/phpunit-bridge             7.2.0  7.2.0  Provides utilities for PHPUnit, especially user deprecation notices management
symfony/polyfill-apcu              1.31.0 1.31.0 Symfony polyfill backporting apcu_* functions to lower PHP versions
symfony/process                    6.4.15 7.2.0  Executes commands in sub-processes
symfony/property-access            6.4.13 7.2.0  Provides functions to read and write from/to an object or array using a simple string notation
symfony/property-info              6.4.16 7.2.1  Extracts information about PHP class' properties using metadata of popular sources
symfony/runtime                    6.4.14 7.2.0  Enables decoupling PHP applications from global state
symfony/security-bundle            6.4.13 7.2.0  Provides a tight integration of the Security component into the Symfony full-stack framework
symfony/serializer                 6.4.15 7.2.0  Handles serializing and deserializing data structures, including object graphs, into array structures or other formats like XML and ...
symfony/stopwatch                  6.4.13 7.2.0  Provides a way to profile code
symfony/string                     6.4.15 7.2.0  Provides an object-oriented API to strings and deals with bytes, UTF-8 code points and grapheme clusters in a unified way
symfony/translation                6.4.13 7.2.0  Provides tools to internationalize your application
symfony/translation-contracts      2.5.4  3.5.1  Generic abstractions related to translation
symfony/twig-bridge                6.4.16 7.2.1  Provides integration for Twig with various Symfony components
symfony/twig-bundle                6.4.13 7.2.0  Provides a tight integration of Twig into the Symfony full-stack framework
symfony/validator                  6.4.16 7.2.0  Provides tools to validate values
symfony/web-link                   6.4.13 7.2.0  Manages links between resources
symfony/web-profiler-bundle        6.4.16 7.2.0  Provides a development tool that gives detailed information about the execution of any request
symfony/webpack-encore-bundle      1.17.2 2.2.0  Integration with your Symfony app & Webpack Encore!
symfony/yaml                       6.4.13 7.2.0  Loads and dumps YAML files

Transitive dependencies not required in composer.json:
symfony/cache                      6.4.16 7.2.1  Provides extended PSR-6, PSR-16 (and tags) implementations
symfony/cache-contracts            3.5.1  3.5.1  Generic abstractions related to caching
symfony/clock                      6.4.13 7.2.0  Decouples applications from the system clock
symfony/config                     6.4.14 7.2.0  Helps you find, load, combine, autofill and validate configuration values of any kind
symfony/dependency-injection       6.4.16 7.2.0  Allows you to standardize and centralize the way objects are constructed in your application
symfony/deprecation-contracts      3.5.1  3.5.1  A generic function and convention to trigger deprecation notices
symfony/doctrine-bridge            6.4.16 7.2.1  Provides integration for Doctrine with various Symfony components
symfony/dom-crawler                6.4.16 7.2.0  Eases DOM navigation for HTML and XML documents
symfony/error-handler              6.4.14 7.2.1  Provides tools to manage errors and ease debugging PHP code
symfony/event-dispatcher           6.4.13 7.2.0  Provides tools that allow your application components to communicate with each other by dispatching events and listening to them
symfony/event-dispatcher-contracts 3.5.1  3.5.1  Generic abstractions related to dispatching event
symfony/filesystem                 6.4.13 7.2.0  Provides basic utilities for the filesystem
symfony/finder                     6.4.13 7.2.0  Finds files and directories via an intuitive fluent interface
symfony/http-client-contracts      3.5.1  3.5.2  Generic abstractions related to HTTP clients
symfony/http-foundation            6.4.16 7.2.0  Defines an object-oriented layer for the HTTP specification
symfony/http-kernel                6.4.16 7.2.1  Provides a structured process for converting a Request into a Response
symfony/messenger                  6.4.16 7.2.1  Helps applications send and receive messages to/from other applications or via message queues
symfony/monolog-bridge             6.4.13 7.2.0  Provides integration for Monolog with various Symfony components
symfony/options-resolver           6.4.16 7.2.0  Provides an improved replacement for the array_replace PHP function
symfony/password-hasher            6.4.13 7.2.0  Provides password hashing utilities
symfony/polyfill-intl-grapheme     1.31.0 1.31.0 Symfony polyfill for intl's grapheme_* functions
symfony/polyfill-intl-icu          1.31.0 1.31.0 Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-intl-idn          1.31.0 1.31.0 Symfony polyfill for intl's idn_to_ascii and idn_to_utf8 functions
symfony/polyfill-intl-normalizer   1.31.0 1.31.0 Symfony polyfill for intl's Normalizer class and related functions
symfony/polyfill-mbstring          1.31.0 1.31.0 Symfony polyfill for the Mbstring extension
symfony/polyfill-php83             1.31.0 1.31.0 Symfony polyfill backporting some PHP 8.3+ features to lower PHP versions
symfony/routing                    6.4.16 7.2.0  Maps an HTTP request to a set of configuration variables
symfony/security-acl               3.3.4  3.3.4  Symfony Security Component - ACL (Access Control List)
symfony/security-core              6.4.16 7.2.0  Symfony Security Component - Core Library
symfony/security-csrf              6.4.13 7.2.0  Symfony Security Component - CSRF Library
symfony/security-http              6.4.15 7.2.1  Symfony Security Component - HTTP Integration
symfony/service-contracts          3.5.1  3.5.1  Generic abstractions related to writing services
symfony/templating                 6.4.13 6.4.13 Provides all the tools needed to build any kind of template system
symfony/var-dumper                 6.4.15 7.2.0  Provides mechanisms for walking through any arbitrary PHP variable
symfony/var-exporter               6.4.13 7.2.0  Allows exporting any serializable PHP data structure to plain PHP code

PHP version

$ php -v
PHP 8.3.14 (cli) (built: Nov 21 2024 19:22:48) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.14, Copyright (c) Zend Technologies
    with Zend OPcache v8.3.14, Copyright (c), by Zend Technologies
    with Xdebug v3.3.2, Copyright (c) 2002-2024, by Derick Rethans

Subject

If you configure multiple hosts different from localhost, localization stops working. However, if there is only one host (localhost) and no host-based restrictions in the routes, locale switching works as expected. When there are no host-based restrictions, the PageBundle correctly understands that the locale (e.g., de) is a locale. But if there are two different hosts, it somehow ignores the locale specified in the URL and instead tries to find a route that matches the locale name.

Minimal repository with the bug

I repeated all the steps on the fresh Symfony 6.4 version. If needed, I can upload these changes to Git, but I don't think it's necessary or will be of much help.

Steps to reproduce

  1. Create multiple sites. For example:
    | name | relative_path | host | locale |
    | First site EN | | first-site.local | en |
    | First site DE | /de | first-site.local | de |
    | Second site EN | | second-site.local | en |
    | Second site DE | /de | second-site.local | de |

  2. Configure routes and bind them to different hosts

  3. Run the commands:

  • ./bin/console sonata:page:update-core-routes
  • ./bin/console sonata:page:create-snapshots
  1. Navigate to any page by specifying its locale, for example: first-site.local/de/test-page (or go to the page and switch the language while on it.)

Expected results

The page opens based on the locale specified in the URL.

Actual results

Error: No route found for "GET http://first-site.local/de/test-page"

@vityachis
Copy link
Author

If someone encounters a similar problem, my solution is to modify the function \Sonata\PageBundle\Site\BaseSiteSelector::getSites() approximately like this (this is a rough example that can and should be improved):

    protected function getSites(Request $request): array
    {
        $pathInfo = $request->getPathInfo();
        $pathLocale = explode('/', $pathInfo)[1] ?? 'en';
        $locale = in_array($pathLocale, ['en', 'de']) ? $pathLocale : 'en';

        return $this->siteManager->findBy([
            'host' => [$request->getHost(), 'localhost'],
            'locale' => $request->get('_locale', $locale),
            'enabled' => true,
        ], [
            'isDefault' => 'DESC',
        ]);
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant