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'}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog