dsqe46004 2017-04-12 13:24
浏览 35
已采纳

Symfony 3:FOSSUserBundle得到一个JsonResponse而不是重定向

On my Symnfony3 project I noticed that during registration some events are generated where I can override the response. eg. Instead of rendering the default twig template and redirect to just return a JsonResponse with a successMessage.

Therefore I did the following Event Subscriber:

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;
    }
}

Also on my services.yml I have put the following:

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

And the command

In order to override on how the response will get returned but somehow it fails to do so and redirects to the default page. What I try to acheive it to perform the registration via ajax call instead of rendering the registration page and redirecting.

  • 写回答

2条回答 默认 最新

  • dongmu1951 2017-04-12 18:18
    关注

    You should do this steps:

    First of all you should use kernel.event_subscriber instead of app.user_register.subscriber when you define the event subscriber therfore your subscriber will be defined like that:

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

    To the services.yml.

    Furthermore the getSubscribedEvents must return the array of the listeners. Also the FOSUserEvents::REGISTRATION_COMPLETED MUST Have a listener even if it isdoes not have an implementation, if you do not want a listener just comment the like.

    In the end your listener shuld be implemented like that:

    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;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!