2
votes

I'm using Symfony 3.3 in conjunction with the JMSI18nRoutingBundle.

The bundle puts the user-locale in front of the routes. This works fine, untill I have to login or logout, these DO NOT work.

I always get the following error:

Unable to find the controller for path "/en/admin/logout". The route is wrongly configured.

This is part of my security.yml covering the login/logout:

security:
    firewalls:
        main:
        anonymous: ~
        pattern: ^.*/admin
        form_login:
            login_path: /{_locale}/admin/login
            check_path: /{_locale}/admin/login
            [...]
        logout:
            path: /{_locale}/admin/logout
            target: /{_locale}/admin/login


access_control:
    - { path: ^/admin/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/admin, roles: ROLE_ADMIN }

For the login_path, check_path, etc. I've also tried instead of {_locale}, .*, and just a slash (/) but none of these work.

The admin-logout is configured in my routing.yml:

admin-logout:
    path: /admin/logout

This gets rendered in twig properly, when using path('admin-logout'), I get a URL with the locale in it, but when I click it, it just displays the above error message again. Login also doesn't work, I can however display the login page, but after entering credentials, it just refreshes the page without having logged in.

Anyone have any idea how to get the login/logout working again?


EDIT:

Thanks to nifr I could solve it, I should've used the route name, not the path in the security.yml, this is what I ended up with:

security:
    firewalls:
        main:
        anonymous: ~
        pattern: admin
        form_login:
            login_path: admin-login
            check_path: admin-login
        [...]
        logout:
            path: admin-logout
            target: admin-login

     [...]

I did not change anything in my routing.yml, as the JMSI18nRoutingBundle already puts the locale placeholder in front of the route (if you do put {_locale} there, you end up with routes like: /en/en/admin/)

1

1 Answers

1
votes

login_path and check_path don't support placeholders when using paths (i.e. /xy/{z} ).

You need to use the route's names plus define a default value for any placeholder.

The login_path and check_path can also be route names (but cannot have mandatory wildcards - e.g. /login/{foo} where foo has no default value).

(Symfony Documentation - How to Build a Traditional Login Form)

The following route definition and security configuration example would work:

# routing.yml
login_route_name:
  path:      /{_locale}/login
  defaults:
    _locale: 'en'
    _controller: app.controller_service.security:login
login_check_route_name:
  path:      /{_locale}/login/check
  defaults:
    _locale: 'en'
    _controller: app.controller_service.security:loginCheck

# security.yml
security:
  firewalls:
    firewall_name:
      # [..]
      form_login:
        login_path: login_route_name
        check_path: login_check_route_name

When using JMSi18nBundle you can find the names of your login/check routes using the debug:router command. For example:

bin/console debug:router | grep -i login