Symfony2 - FOSUserBundle - Multiple Login Locations

后端 未结 3 649
青春惊慌失措
青春惊慌失措 2021-01-06 01:14

I\'m using the FOSUserBundle and I require the ability to login from 2 different routes(or more). These routes will have different templates and also login to different area

相关标签:
3条回答
  • 2021-01-06 01:35

    I was stucked with the same question for a while and then i created a solution on my own. I knew there must be an easy solution...

    I've submitted pull request which allows you to create new login templates more easily. Check the pull request here: https://github.com/FriendsOfSymfony/FOSUserBundle/pull/1186.

    There is also another quite easy way how to achieve this. Extend SecurityController and change renderLogin method with following content

    protected function renderLogin(array $data, $template)
    {
        return $this->container->get('templating')->renderResponse('YourBundle:Security:login.html.twig');
    }
    

    Then create a route to your newly created controller:

    admin.login:
        pattern: /admin/login
        defaults: { _controller: YourBundle:Security:login }
    

    After this only you have to do is to alter your security config accordingly. Change your form_login login_path to /admin/login and you are good to go.

    0 讨论(0)
  • 2021-01-06 01:35

    Here's what I eventually came up with, basically the both use the same firewall and share the same context so when I login via the normal login they also get access to the admin(assuming they are admin)

    firewalls:
        admin:
            context:            site
            switch_user:        true
            pattern:            /admin(.*)
            form_login:
                provider:       fos_userbundle
                login_path:     /admin/login
                success_handler: admin_authentication_handler
                use_forward:    false
                check_path:     /admin/login_check
                failure_path:   null
                use_referer:    true
                always_use_default_target_path: true
                default_target_path: /admin/
            logout:
                path:           /admin/logout
                target:         /admin/login
            anonymous:    true
        public:
            pattern:   ^/
            context:            site
            form_login:
                login_path:     /login
                success_handler: authentication_handler
                failure_handler: authentication_handler
                provider: fos_userbundle
            anonymous: true
            logout: true
    

    Of course if you need to make an AJAX login box you need to override the success and failure handlers and check to see if the request is a XmlHttpRequest and return the result of the login.

    0 讨论(0)
  • 2021-01-06 01:42

    Could you post your template?

    Have you edited the path correctly in the template?

    <form action="{{ path('form_submit') }}" method="post" {{ form_enctype(form) }}>
    

    You want to send the form to the correct controller.

    0 讨论(0)
提交回复
热议问题