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 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)