Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2/FOSUserBundle - Route issues with multiple bundles

Similar to this problem, I need a separate login for both the admin and front end bundles of my site. The admin is actually a separate bundle located in vendors.

Right now, my routing looks like:

app/config/routing.yml:

AcmeSiteBundle:
    resource: "@SiteBundle/Resources/config/routing.yml"
    prefix:   /

AcmeAdminBundle:
    resource: "@AdminBundle/Resources/config/routing.yml"
    prefix:   /admin/

Both of the bundles' individual routing.yml files have:

fos_user_security:
    resource: "@FOSUserBundle/Resources/config/routing/security.xml"

fos_user_profile:
    resource: "@FOSUserBundle/Resources/config/routing/profile.xml"
    prefix: /profile

fos_user_register:
    resource: "@FOSUserBundle/Resources/config/routing/registration.xml"
    prefix: /register

fos_user_security_login:
    pattern:  /login
    defaults: { _controller: FOSUserBundle:Security:login }

fos_user_security_check:
    pattern:  /login_check
    defaults: { _controller: FOSUserBundle:Security:check }

fos_user_security_logout:
    pattern:  /logout
    defaults: { _controller: FOSUserBundle:Security:logout }

And my firewalls in security.yml:

firewalls:
    main:
        context: site
        pattern: ^/admin/
        form_login:
            provider: fos_userbundle
            csrf_provider: form.csrf_provider
            login_path:  /admin/login
            check_path:  /admin/login_check
        logout:
            path: /admin/logout
        anonymous:    true

    frontend:
        context: site
        pattern: ^/
        form_login:
            provider: fos_userbundle
            csrf_provider: form.csrf_provider
            login_path: /login
            check_path: /login_check
        logout:
            path: /logout
        anonymous: true

The problem is that the front end's automatically generated login links point to /admin/login rather than just /login, which is not what I want to have happen.

So, how can I make it use the /admin/* links when I'm there, but just the / links when I'm on the front end? I need to keep their contexts linked as people logged into the admin side should stay logged in on the front end.


EDIT: I renamed my routes as the following:

SiteBundle's routing.yml (same as before):

fos_user_security:
    resource: "@FOSUserBundle/Resources/config/routing/security.xml"

fos_user_profile:
    resource: "@FOSUserBundle/Resources/config/routing/profile.xml"
    prefix: /profile

fos_user_register:
    resource: "@FOSUserBundle/Resources/config/routing/registration.xml"
    prefix: /register

fos_user_security_login:
    pattern:  /login
    defaults: { _controller: FOSUserBundle:Security:login }

fos_user_security_check:
    pattern:  /login_check
    defaults: { _controller: FOSUserBundle:Security:check }

fos_user_security_logout:
    pattern:  /logout
    defaults: { _controller: FOSUserBundle:Security:logout }

AdminBundle's routing.yml:

_admin_user_security:
    resource: "@FOSUserBundle/Resources/config/routing/security.xml"

_admin_user_profile:
    resource: "@FOSUserBundle/Resources/config/routing/profile.xml"
    prefix: /profile

_admin_user_security_login:
    pattern:  /login
    defaults: { _controller: FOSUserBundle:Security:login }

_admin_user_security_check:
    pattern:  /login_check
    defaults: { _controller: FOSUserBundle:Security:check }

_admin_user_security_logout:
    pattern:  /logout
    defaults: { _controller: FOSUserBundle:Security:logout }

$ app/console router:debug shows:

fos_user_registration_register         ANY    ANY    ANY  /register/
fos_user_registration_check_email      GET    ANY    ANY  /register/check-email
fos_user_registration_confirm          GET    ANY    ANY  /register/confirm/{token}
fos_user_registration_confirmed        GET    ANY    ANY  /register/confirmed
fos_user_security_login                ANY    ANY    ANY  /admin/login
fos_user_security_check                ANY    ANY    ANY  /admin/login_check
fos_user_security_logout               ANY    ANY    ANY  /admin/logout
fos_user_profile_show                  GET    ANY    ANY  /admin/profile/
fos_user_profile_edit                  ANY    ANY    ANY  /admin/profile/edit
_admin_user_security_login             ANY    ANY    ANY  /admin/login
_admin_user_security_check             ANY    ANY    ANY  /admin/login_check
_admin_user_security_logout            ANY    ANY    ANY  /admin/logout

As you can see, the only route that is correct is for user registration, and that's only because it's located just in SiteBundle's routing.yml.

like image 874
Major Productions Avatar asked Oct 04 '22 06:10

Major Productions


1 Answers

Figured it out:

Since the FOSUserBundle was registered with my admin firewall in config.yml, it kept defaulting to its routes even after I renamed them. So, turning them back to fos_*, and renaming the site bundle's routes seems to have fixed it.

like image 157
Major Productions Avatar answered Oct 13 '22 11:10

Major Productions