dsdf64562672 2016-11-28 20:38 采纳率: 0%
浏览 40
已采纳

未触发Symfony2 FOSuserBundle事件REGISTRATION_COMPLETED

For my project, I need to redirect the user after registration. In order to achieve that, I created an EventListener as described below :

My Event Listener :

namespace UserBundle\EventListener;

use FOS\UserBundle\FOSUserEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class RegistrationConfirmListener implements EventSubscriberInterface
{
    private $router;

    public function __construct(UrlGeneratorInterface $router)
    {
        $this->router = $router;
    }

    /**
     * {@inheritDoc}
     */
    public static function getSubscribedEvents()
    {
        return array(
            FOSUserEvents::REGISTRATION_CONFIRM => 'onRegistrationConfirm'
        );
    }

    public function onRegistrationConfirm(GetResponseUserEvent $event)
    {
        $url = $this->router->generate('standard_user_registration_success');
        $event->setResponse(new RedirectResponse($url));
    }
}

I registered it as a service in my service.yml :

services:
    rs_user.registration_complet:
        class: UserBundle\EventListener\RegistrationConfirmListener
        arguments: [@router]
        tags:
            - { name: kernel.event_subscriber }

And I need to use it in my RegistrationController but I don't understand how to trigger it. Here in my registerAction :

public function registerAction(Request $request)
{
        $em = $this->get('doctrine.orm.entity_manager');
        //Form creation based on my user entity
        $user = new StandardUser();
        $form = $this->createForm(RegistrationStandardUserType::class, $user);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $user       ->setEnabled(true);
            $em         ->persist($user);
            $em         ->flush();
            if ($user){
                $dispatcher = $this->get('event_dispatcher');
                $dispatcher->dispatch(FOSUserEvents::REGISTRATION_CONFIRM);
            }
        }

    return $this->render('UserBundle:Registration:register.html.twig', array(
            'form' => $form->createView()
    ));
}

I don't understand the Symfony2 documentation about the subject neither what I need to pass to the ->dispatch() function to trigger my event.

[EDIT] I get this error when I register my user :

Type error: Argument 1 passed to
UserBundle\EventListener\RegistrationConfirmListener::onRegistrationConfirm()
must be an instance of UserBundle\EventListener\GetResponseUserEvent,
instance of Symfony\Component\EventDispatcher\Event given
500 Internal Server Error - FatalThrowableError
  • 写回答

1条回答 默认 最新

  • dosi8657 2016-11-28 20:44
    关注

    Your listener declares that it is subscribed to FOSUserEvents::REGISTRATION_CONFIRM but you are dispatching FOSUserEvents::REGISTRATION_COMPLETED. To trigger it you need to dispatch a FOSUserEvents::REGISTRATION_CONFIRM event

    edit to match your edit, you need to pass the event in your services tags:

    - { name: 'kernel.event_subscriber', event: 'fos_user.registration.confirm'}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 求给定范围的全体素数p的(p-2)的连乘积
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页