php - Symfony 3.2 FOSUserBundle Ajax 登录

标签 php ajax symfony fosuserbundle

在 FOSUserBundle 中,我想在用户登录后将用户重定向到 fos_user_profile_show 路由而不加载页面(AJAX 查询)。我卡在了这一点上。论坛里有类似的话题,但是已经过时了。

AuthenticationHandler.php

<?php

namespace AppBundle\Handler;

use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;

/**
 * Class AuthenticationHandler
 * @package AppBundle\Handler
 */
class AuthenticationHandler implements AuthenticationSuccessHandlerInterface, AuthenticationFailureHandlerInterface
{
    /**
     * @var RouterInterface
     */
    private $router;
    /**
     * @var Session
     */
    private $session;


    /**
     * AuthenticationHandler constructor.
     * @param RouterInterface $router
     * @param Session $session
     */
    public function __construct(RouterInterface $router, Session $session)
    {
        $this->router = $router;
        $this->session = $session;
    }

    /**
     * @param Request $request
     * @param TokenInterface $token
     * @return JsonResponse|RedirectResponse
     */
    public function onAuthenticationSuccess(Request $request, TokenInterface $token)
    {

        if ($request->isXmlHttpRequest()) {
            return new JsonResponse(array('success' => true));
        }
        else {
            $url = $this->router->generate('fos_user_profile_show');
            return new RedirectResponse($url);
        }

    }

    /**
     * @param Request $request
     * @param AuthenticationException $exception
     * @return JsonResponse|RedirectResponse
     */
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
    {
        if ($request->isXmlHttpRequest()) {
            return new JsonResponse(array('success' => false, 'message' => $exception->getMessage()));
        } else {
            $request->get('session')->set(Security::AUTHENTICATION_ERROR, $exception);
            return new RedirectResponse($this->router->generate('fos_user_security_login'));
        }
    }
}

服务.yml

app.security.authentication_handler:
    class: AppBundle\Handler\AuthenticationHandler
    public: false
    arguments:
            - "@router"
            - "@session"

安全.yml

firewalls:
    main:
        pattern: ^/
        form_login:
            provider: fos_userbundle
            csrf_token_generator: security.csrf.token_manager
            check_path: fos_user_security_check
            success_handler: app.security.authentication_handler
            failure_handler: app.security.authentication_handler
        logout:       true
        anonymous:    true

login_content.html.twig

<script>
    $(document).ready(function(){

        $('#_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.success == false) {
                        console.log(data.message);
                    } else {
                        window.location.href = data.targetUrl;
                    }
                }
            });

    });
</script>

最佳答案

我是这样修复的;

$url = $this->router->generate('fos_user_profile_show');
return new JsonResponse(array('success' => true));

顺便说一下,感谢所有这些代码。

关于php - Symfony 3.2 FOSUserBundle Ajax 登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43879882/

相关文章:

php - CakePHP 重定向状态代码 404

php - 用php删除xml中标签值之间的空格

javascript - Googlebot 什么时候执行 javascript?

php - Symfony 2 部署 git 文件

php - 在 php 中读取文本文件时出错

javascript - 我们可以从 html 文本框中调用 java 函数吗?

javascript - 带有 Google Invisible Recaptcha 的 Ajax 表单

symfony - 安全 : password check removal and custom User Provider

symfony - Symfony2 中的 Doctrine2 (Doctrine 2.1) 急切加载

php - 在多个数组中循环多个 SQL 查询