加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – Symfony 3.2 FOSUserBundle Ajax登录

发布时间:2020-12-13 16:09:13 所属栏目:PHP教程 来源:网络整理
导读:在FOSUserBundle中,我想在用户登录后重定向用户而不加载页面( AJAX查询)到fos_user_profile_show路由.我坚持到这一点.论坛中有类似的主题,但它们已经过时了. AuthenticationHandler.php ?phpnamespace AppBundleHandler;use SymfonyComponentHttpFoundati
在FOSUserBundle中,我想在用户登录后重定向用户而不加载页面( AJAX查询)到fos_user_profile_show路由.我坚持到这一点.论坛中有类似的主题,但它们已经过时了.

AuthenticationHandler.php

<?php

namespace AppBundleHandler;

use SymfonyComponentHttpFoundationJsonResponse;
use SymfonyComponentHttpFoundationRedirectResponse;
use SymfonyComponentRoutingRouterInterface;
use SymfonyComponentHttpFoundationSessionSession;
use SymfonyComponentSecurityCoreAuthenticationTokenTokenInterface;
use SymfonyComponentSecurityCoreExceptionAuthenticationException;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentSecurityCoreSecurity;
use SymfonyComponentSecurityHttpAuthenticationAuthenticationSuccessHandlerInterface;
use SymfonyComponentSecurityHttpAuthenticationAuthenticationFailureHandlerInterface;

/**
 * Class AuthenticationHandler
 * @package AppBundleHandler
 */
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'));
        }
    }
}

services.yml

app.security.authentication_handler:
    class: AppBundleHandlerAuthenticationHandler
    public: false
    arguments:
            - "@router"
            - "@session"

security.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));

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读