Symfony 3:FOSSUserBundle获取JsonResponse而不是重定向

时间:2017-04-12 13:24:06

标签: php ajax symfony fosuserbundle

在我的Symnfony3项目中,我注意到在注册过程中会生成一些事件,我可以覆盖响应。例如。而不是渲染默认的树枝模板并重定向只返回带有成功消息的JsonResponse。

因此,我做了以下事件订阅者:

namespace AppBundle\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\FormEvent;
use AppBundle\Constants\AjaxJsonResponseConstants;
use Symfony\Component\HttpFoundation\JsonResponse;
use FOS\UserBundle\Event\FilterUserResponseEvent;

class UserRegistrationResponseChanger implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        $subscribedEvents=[
//              FOSUserEvents::REGISTRATION_INITIALIZE=>[],
                FOSUserEvents::REGISTRATION_COMPLETED=>[],
                FOSUserEvents::REGISTRATION_SUCCESS=>["setJsonResponseOnSuccess",-1],
                FOSUserEvents::REGISTRATION_FAILURE=>["setJsonResponseOnFailure",-1],
//              FOSUserEvents::REGISTRATION_CONFIRM=>[],
//              FOSUserEvents::REGISTRATION_CONFIRMED=>[]
        ];
    }

    public function setJsonResponseOnSuccess(FormEvent $formEvent)
    {
        $response=['status'=>AjaxJsonResponseConstants::AJAX_ACTION_SUCCESS,'message'=>"User Sucessfully Registered please check your mail."];
        $response=new JsonResponse($response);
        $formEvent->setResponse($response);

        return $response;
    }

    public function setJsonResponseOnFailure(FormEvent $formEvent)
    {
        $response=['status'=>AjaxJsonResponseConstants::AJAX_ACTION_FAIL,'message'=>"You cannot register please try again later"];
        $response=new JsonResponse($response);
        $formEvent->setResponse($response);

        return $response;
    }
}

同样在我的services.yml上,我提出了以下内容:

 app.user_register.subscriber:
  class: AppBundle\EventSubscriber\UserRegistrationResponseChanger
  tags:
   - { name: app.user_register.subscriber }

命令

为了覆盖响应将如何返回,但不知何故它没有这样做并重定向到默认页面。我尝试通过ajax调用来执行注册,而不是渲染注册页面和重定向。

2 个答案:

答案 0 :(得分:1)

如果您有注册确认(FOSUserBundle中的默认行为),则应优先考虑REGISTRATION_SUCCESS事件,请参阅http://symfony.com/doc/master/bundles/FOSUserBundle/controller_events.html#registration-success-listener-with-enabled-confirmation-at-the-same-time

服务定义必须如下:

#app/config/services.yml
app.security_registration_success:
    class: Path\To\Your\EventListener\RegistrationSuccessListener
    tags:
        - { name: kernel.event_subscriber }

注册成功监听器的示例:

<?php

declare(strict_types=1);

namespace Path\To\Your\EventListener;

use FOS\UserBundle\Event\FormEvent;
use FOS\UserBundle\FOSUserEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\JsonResponse;

class RegistrationSuccessListener implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [FOSUserEvents::REGISTRATION_SUCCESS => [['onRegistrationSuccess', -10]]];
    }

    public function onRegistrationSuccess(FormEvent $event): void
    {
        $event->setResponse(new JsonResponse());
    }
}

答案 1 :(得分:0)

您应该执行以下步骤:

首先,当您定义事件订阅者时,您应该使用kernel.event_subscriber而不是app.user_register.subscriber来定义您的订阅者:

 app.user_register.subscriber:
  class: AppBundle\EventSubscriber\UserRegistrationResponseChanger
  tags:
   - { name: kernel.event_subscriber }

services.yml

此外,getSubscribedEvents必须返回侦听器的数组。此外,FOSUserEvents::REGISTRATION_COMPLETED 必须如果您不希望侦听器只是评论,那么即使它没有实现,也要有一个监听器。

最后,你的听众应该像这样实施:

namespace AppBundle\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\FormEvent;
use AppBundle\Constants\AjaxJsonResponseConstants;
use Symfony\Component\HttpFoundation\JsonResponse;
use FOS\UserBundle\Event\FilterUserResponseEvent;

class UserRegistrationResponseChanger implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        $subscribedEvents=[
//              FOSUserEvents::REGISTRATION_INITIALIZE=>[],
//              FOSUserEvents::REGISTRATION_COMPLETED=>[],
                FOSUserEvents::REGISTRATION_SUCCESS=>["setJsonResponseOnSuccess",-1],
                FOSUserEvents::REGISTRATION_FAILURE=>["setJsonResponseOnFailure",-1],
//              FOSUserEvents::REGISTRATION_CONFIRM=>[],
//              FOSUserEvents::REGISTRATION_CONFIRMED=>[]
        ];

        return $subscribedEvents;
    }

    public function setJsonResponseOnSuccess(FormEvent $formEvent)
    {
        $response=['status'=>AjaxJsonResponseConstants::AJAX_ACTION_SUCCESS,'message'=>"User Sucessfully Registered please check your mail."];
        $response=new JsonResponse($response);
        $formEvent->setResponse($response);

        return $response;
    }

    public function setJsonResponseOnFailure(FormEvent $formEvent)
    {
        $response=['status'=>AjaxJsonResponseConstants::AJAX_ACTION_FAIL,'message'=>"You cannot register please try again later"];
        $response=new JsonResponse($response);
        $formEvent->setResponse($response);

        return $response;
    }
}