34

I've installed FOSUserBundle and I'd like to customize the urls to be /account/login, /account/register, /account/logout instead of /login, /register, /logout

I know I can modify the routing config of the bundle, but it doesn't seem to be the proper way.

Ahmed Siouani
  • 13,701
  • 12
  • 61
  • 72
petekaner
  • 8,071
  • 5
  • 29
  • 52
  • 1
    or copy the whole routing xml's from the fosUserBundle to your app folder and customize them there or change the prefix they suggest in step6 of the [install docs](https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/index.md#step-6-import-fosuserbundle-routing-files) – acrobat Oct 26 '13 at 15:50
  • 2
    No job for `.htaccess` - completely wrong direction !! you can either copy the routes over and include only the changed ones or just override single ones as described in my answer. – Nicolai Fröhlich Oct 26 '13 at 16:15

1 Answers1

81

How to override / change FOSUserBundle's routes

You can override i.e the /register route in your app/config/routing.yml by re-declaring it after importing FOSUserBundle's XML routes as resources.

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

# ...

fos_user_registration_register:
    path:      /account/register
    defaults:  { _controller: FOSUserBundle:Registration:register }

... or just change the prefix when importing:

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

# no need to override the route

The same goes for /login and /logout :

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

# ...

fos_user_security_login:
    path:      /account/login
    defaults:  { _controller: FOSUserBundle:Security:login, _method: POST }

fos_user_security_logout:
    path:      /account/login
    defaults:  { _controller: FOSUserBundle:Security:logout, _method: POST }

Another way to override login and logout url's

login and logout paths can aswell be configured directly in your app/config/security.yml:

security:
    firewalls: 
        your_firewall:
            # ...
            form_login:
                login_path: /account/login  # instead of fos_user_security_login
                # ...
            logout:
                path: /account/logout       # instead of fos_user_security_logout
                # ...

List of all of FOSUserBundle's routes in YAML format

You can directly change and then include these in your app/config/routing.yml ( no need to import the ones the bundle provides as resources then) ... or put them all into a single file and include that one as a resource...

# -> from @FOSUserBundle/Resources/routing/change_password.xml

fos_user_change_password:
    pattern: /profile/password/change
    defaults: { _controller: FOSUserBundle:ChangePassword:changePassword }
    requirements:
        _method: GET|POST

# -> from @FOSUserBundle/Resources/routing/group.xml

fos_user_group_list:
    pattern: /groups/list
    defaults: { _controller: FOSUserBundle:Group:list }
    requirements:
        _method: GET

fos_user_group_new:
    pattern: /groups/new
    pattern:
    defaults: { _controller: FOSUserBundle:Group:new }
    requirements:
        _method: GET

fos_user_group_show:
    pattern: /groups/{groupname}
    defaults: { _controller: FOSUserBundle:Group:show }
    requirements:
        _method: GET

fos_user_group_edit:
    pattern: /groups/{groupname}/edit
    defaults: { _controller: FOSUserBundle:Group:edit }
    requirements:
        _method: GET|POST

fos_user_group_delete:
    pattern: /groups/{groupname}/delete
    defaults: { _controller: FOSUserBundle:Group:delete }
    requirements:
        _method: GET

# -> from @FOSUserBundle/Resources/routing/profile.xml

fos_user_profile_show:
    pattern: /profile/show
    defaults: { _controller: FOSUserBundle:Profile:show }
    requirements:
        _method: GET

fos_user_profile_edit:
    pattern: /profile/edit
    defaults: { _controller: FOSUserBundle:Profile:edit }
    requirements:
        _method: GET|POST

# -> from @FOSUserBundle/Resources/routing/registration.xml

fos_user_registration_register:
    pattern: /registration
    defaults: { _controller: FOSUserBundle:Registration:register }
    requirements:
        _method: GET|POST

fos_user_registration_check_email:
    pattern: /registration/check-email
    defaults: { _controller: FOSUserBundle:Registration:checkEmail }
    requirements:
        _method: GET

fos_user_registration_confirm:
    pattern: /registration/confirm/{token}
    defaults: { _controller: FOSUserBundle:Registration:confirm }
    requirements:
        _method: GET

fos_user_registration_confirmed:
    pattern: /registration/confirmed
    defaults: { _controller: FOSUserBundle:Registration:confirmed }
    requirements:
        _method: GET

# -> from @FOSUserBundle/Resources/routing/resetting.xml

fos_user_resetting_request:
    pattern: /profile/password/reset
    defaults: { _controller: FOSUserBundle:Resetting:request }
    requirements:
        _method: GET

fos_user_resetting_send_email:
    pattern: /profile/password/reset
    defaults: { _controller: FOSUserBundle:Resetting:sendEmail }
    requirements:
        _method: POST

fos_user_resetting_check_email:
    pattern: /profile/password/reset/check-email
    defaults: { _controller: FOSUserBundle:Registration:checkEmail }
    requirements:
        _method: GET

fos_user_resetting_reset:
    pattern: /profile/password/reset/{token}
    defaults: { _controller: FOSUserBundle:Registration:reset }
    requirements:
        _method: GET|POST

# -> from @FOSUserBundle/Resources/routing/security.xml

fos_user_security_login:
    pattern: /login
    defaults: { _controller: FOSUserBundle:Security:login }
    requirements:
        _method: GET|POST

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

fos_user_security_logout:
    pattern: /logout
    defaults: { _controller: FOSUserBundle:Security:logout }
    requirements:
        _method: GET|POST
Boschman
  • 825
  • 1
  • 10
  • 17
Nicolai Fröhlich
  • 51,330
  • 11
  • 126
  • 130
  • 2
    you need to setup login path and logout path app/config/security.yml for correct redirection. that step is mandatory I think – Straw Hat Nov 08 '14 at 06:39
  • 3
    The `pattern` and `_method` are deprecated since version 2.2. – Iago May 12 '15 at 03:34
  • Thanks for a great answer. I've corrected a couple of minor mistakes and added an Symfony 3 version (leaving the Symfony 2 version there). It's just waiting to be peer reviewed. – Luke Cousins May 26 '16 at 08:37
  • nifir,you have errors in your path. In the "Resetting" group, you use `FOSUserBundle:Registration`, and this will give an error on all paths. – Akairis Aug 30 '15 at 19:56