Symfony 2 FOS User Bundle Bootstrap modal AJAX Login

前端 未结 2 1839
北海茫月
北海茫月 2021-02-08 06:20

Has anyone already built a login form inside a Bootstrap modal with Symfony 2 and FOS User Bundle ?

Here is what I have now :

src/Webibli/UserBundle/Reso

2条回答
  •  囚心锁ツ
    2021-02-08 07:00

    I guess what youre looking for is this: Symfony2 ajax login.

    your javascript would look sth. like this:

    $('#_submit').click(function(e){
            e.preventDefault();
            $.ajax({
                type        : $('form').attr( 'method' ),
                url         : $('form').attr( 'action' ),
                data        : $('form').serialize(),
                success     : function(data, status, object) {
                    if (data.sucess == false) {
                        $('.modal-body').prepend('
    ').html(data.message); } else { window.location.href = data.targetUrl; } } });

    You also have to modify the isXmlHttpRequest-part of your onAuthenticationSuccess-Method:

    [...]
    if ($request->isXmlHttpRequest()) {
                $targetUrl = $request->getSession()->get('_security.target_path');
                $result = array('success' => true, 'targetUrl' => targetUrl );
                $response = new Response(json_encode($result));
                $response->headers->set('Content-Type', 'application/json');
                return $response;
            }
    [...]
    

提交回复
热议问题