Search code examples
phpsymfonyfosuserbundle

Symfony 4 FOSUserBundle Custom Routes - Add prefix and subpages


Building an app using Symfony 4 and FOSUserBundle.

(User's profile UI is a set of tabs.)

I want to have a sub-route for each tab in the profile, so each tab is on it's own page/route.

eg myapp.com/profile/dashboard

eg myapp.com/profile/bookings

What are the basic steps to do this?

PS What if I wanted to drop the "/profile" part of the route:

eg myapp.com/dashboard

or use a dynamic one such as the user account number "/11123"

eg myapp.com/11123/dashboard


Solution

  • There are no special steps. You can totally ignore FOS routes by just commenting its definition inside config/routes/fos_user.yaml:

    #fos_user:
    #    resource: "@FOSUserBundle/Resources/config/routing/all.xml"
    

    you may also ignore some parts (comment it out) and use other parts of FOS routes:

    #fos_user_profile:
    #    resource: "@FOSUserBundle/Resources/config/routing/profile.xml"
    
    fos_user_register:
        resource: "@FOSUserBundle/Resources/config/routing/registration.xml"
        prefix: /security
    
    fos_user_login:
        resource: "@FOSUserBundle/Resources/config/routing/security.xml"
        prefix: /security
    

    then just put your own routes inside config/routes/annotations.yaml:

    /myapp.com/profile/dashboard/
    /myapp.com/profile/bookings/
    

    and process it as you wish. If you need to override FOS controllers, then check this:

    Overriding Default FOSUserBundle Controllers