symfony / demo

Symfony Demo Application
https://symfony.com/
MIT License
2.46k stars 1.61k forks source link

Default locale not working #1520

Closed Mecanik closed 1 month ago

Mecanik commented 3 months ago

Hello,

I cloned the project several times and tested over and over, it seems that the default locale (en) is not applied with there is no locale specified. This is very bad and will cause a lot off 404's and other issues; shouldn't RedirectToPreferredLocaleSubscriberdo this by default? Or what is the correct solution for this?

Please advise, thanks.

Mecanik commented 2 months ago

Anyone please?

xabbuh commented 2 months ago

it seems that the default locale (en) is not applied with there is no locale specified

I am not sure I understand what you mean with that. Can you please provide some details that will allows us to understand what exactly you are doing and what you expected to happen?

Mecanik commented 2 months ago

it seems that the default locale (en) is not applied with there is no locale specified

I am not sure I understand what you mean with that. Can you please provide some details that will allows us to understand what exactly you are doing and what you expected to happen?

Sure. I copied everything in terms of how the localisation is setup in this demo, into a new 7.1 project. It "works", but not as it should (or I imagine it should work).

For example, I go to:

website.com/ - this will trigger the default Symfony 7 page, although I have a DefaultController.

But if I go to:

website.com/en/ - this will trigger my DefaultController.

The exact same thing happens for other controllers, for example ThingController.

If I go to:

website.com/thing/new/ - this will trigger: No route found for "GET http://.../ads"

But if I go to:

website.com/en/thing/new - this will trigger ThingController.

So the default locale is not being set when there is no locale specified, this is my main issue. The configuration is copied exactly as in this project.

If this is the default behaviour; it's not good; it should add the default locale when one is not specified and redirect.

If this is not the default behaviour; it means I have an issue somewhere and I would appreciate some directions.

Thanks

xabbuh commented 2 months ago

Well, if we are talking about your own application (even if some of its code is borrowed from the demo application), please use on of the Symfony support channels (Slack or StackOverflow). If you experience an issue with the demo itself, we will need more information.

lxregistry commented 2 months ago

Anyone please?

the same problem for me, but I rolled back this commit and now is ok https://github.com/symfony/demo/commit/2955454edb601415b87966bae1ed0c4fda712914

just use prevoius version

xabbuh commented 2 months ago

@lxregistry What does „the same problem“ mean? Do you also have a custom application based on the demo? If not, please answer my questions from https://github.com/symfony/demo/issues/1520#issuecomment-2175613810.

lxregistry commented 2 months ago

a custom application based on the demo

yes, I have a custom application based on the demo, after changes with locales, login routes stopped to work. I do no not know what is wrong, so I rolled back 6 files to previous version.

    - { path: '^/(%app_locales%)/admin/login$', roles: PUBLIC_ACCESS, ips: [xxx.xxx.xxx.xxx/22, ::1], requires_channel: https }
    - { path: '^/(%app_locales%)/admin/logout$', roles: PUBLIC_ACCESS }
    - { path: '^/(%app_locales%)/admin/', roles: ROLE_ADMIN, requires_channel: https }
    - { path: '^/(%app_locales%)/professional/login$', roles: PUBLIC_ACCESS, requires_channel: https }
    - { path: '^/(%app_locales%)/professional/logout$', roles: PUBLIC_ACCESS }
    - { path: '^/(%app_locales%)/professional/', roles: ROLE_COMPANY, requires_channel: https }
    - { path: '^/(%app_locales%)/login$', roles: PUBLIC_ACCESS, requires_channel: https }
    - { path: '^/(%app_locales%)/logout$', roles: PUBLIC_ACCESS }
    - { path: '^/(%app_locales%)/account/', roles: ROLE_USER, requires_channel: https }
    - { path: '^/api/login$', roles: PUBLIC_ACCESS, requires_channel: https }
    - { path: '^/api/', roles: IS_AUTHENTICATED_FULLY, requires_channel: https }
    - { path: '^/login', roles: PUBLIC_ACCESS }
    - { path: '^/connect', roles: PUBLIC_ACCESS }
xabbuh commented 2 months ago

For support with custom applications, please refer to one of the support channels.

Mecanik commented 2 months ago

This is not about the "custom application", this is a bug.

xabbuh commented 2 months ago

Then please be more precise about how one could reproduce the wrong behaviour that you experience with the demo application.

Mecanik commented 2 months ago

Then please be more precise about how one could reproduce the wrong behaviour that you experience with the demo application.

Ok. First of all, what is the purpose of the demo? I believe it is so people understand how to get started with Symfony. As such, it is not possible to use it "as is" for your own project. So what does one do? Start to setup from scratch, following the demo. As I mentioned from the start, following the demo, try to setup localisation in a fresh install and you will see the bug (literally copy over config, nothing fancy).

xabbuh commented 2 months ago

Well, at least for me it is not clear what you were doing to end up with an issue. So if you really want to have someone try to understand the issue you are seeing, I'd suggest that you clone the demo and make the necessary changes that are required and explain what steps one needs to perform with it to see what you are seeing.

xabbuh commented 1 month ago

I am closing as explained here. But we can consider to reopen when we have more information.

Mecanik commented 2 weeks ago

Just to note... this issue occurs when you have your routes defined as default:

controllers:
  resource:
    path: ../src/Controller/
    namespace: App\Controller
  type: attribute
  prefix: /{_locale}
  defaults:
    _locale: "%locale%"

However, in the demo you have the routes defined as:

# These lines define a route using YAML configuration. The controller used by
# the route (FrameworkBundle:Template:template) is a convenient shortcut when
# the template can be rendered without executing any logic in your own controller.
# See https://symfony.com/doc/current/templates.html#rendering-a-template-directly-from-a-route
homepage:
    path: /{_locale}
    controller: Symfony\Bundle\FrameworkBundle\Controller\TemplateController::templateAction
    defaults:
        template: default/homepage.html.twig
        _locale: '%locale%'

controllers:
    resource:
        path: '../src/Controller/'
        namespace: App\Controller
    type: attribute
    prefix: /{_locale}
    defaults:
        _locale: '%locale%'

Which is why the "homepage" loads with the default locale "en"... without a controller, but if you have your own controller, you must define it like:

homepage:
  path: /{_locale}
  controller: App\Controller\DefaultController::index
  defaults:
    template: default/default.html.twig
    _locale: "%locale%"

controllers:
  resource:
    path: ../src/Controller/
    namespace: App\Controller
  type: attribute
  prefix: /{_locale}
  defaults:
    _locale: "%locale%"

Which solves the above issue... however, the /en parameter will be missing from the URL, which will eventually cause canonical issues... indexing issues and so on.

Ideally, there should be a function that checks if there is no default locale using the default routes config, and simply add "en" to it (or whatever is defined). Possibly even a simple .htaccess rewrite rule.

Just to note, this is a problem, and it shouldn't be ignored because someone doesn't "explain right".