【问题标题】:Symfony2 FOSuserBundle event REGISTRATION_COMPLETED is not triggeredSymfony2 FOSuserBundle 事件 REGISTRATION_COMPLETED 未触发
【发布时间】:2016-11-28 20:38:15
【问题描述】:

对于我的项目,我需要在注册后重定向用户。为了实现这一点,我创建了一个EventListener,如下所述:

我的事件监听器:

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

我在我的 service.yml 中将它注册为服务:

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

我需要在我的 RegistrationController 中使用它,但我不明白如何触发它。 在我的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()
    ));
}

我不了解 Symfony2 documentation 关于该主题的内容,也不了解我需要传递给 ->dispatch() 函数以触发我的事件。

[编辑] 注册用户时出现此错误:

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

【问题讨论】:

    标签: php symfony events registration fosuserbundle


    【解决方案1】:

    您的侦听器声明它订阅了FOSUserEvents::REGISTRATION_CONFIRM,但您正在调度FOSUserEvents::REGISTRATION_COMPLETED。要触发它,您需要调度 FOSUserEvents::REGISTRATION_CONFIRM 事件

    编辑以匹配您的编辑,您需要在您的服务中传递事件tags

    - { name: 'kernel.event_subscriber', event: 'fos_user.registration.confirm'}
    

    【讨论】:

    • 我的错,但我已经尝试过了,但出现错误,我更新了我的帖子(抱歉浪费时间)
    • 好的,感谢您的更新。该事件现在已按照您的帖子中的描述传递,但我仍然有错误。我认为这可能是我发送它的方式,但我仍然不知道......
    • 您可以删除方法中的声明以确保方法运行,即使它不是您期望的确切事件对象。即,而不是public function onRegistrationConfirm(GetResponseUserEvent $event)public function onRegistrationConfirm($event) - 或声明它正在返回的事件,根据你在做什么应该没问题
    • 这样做,我得到:尝试调用类“Symfony\Component\EventDispatcher\Event”的未定义方法“setResponse”。在我的听众身上。我现在知道我的 eventListener 被触发了,我只需要重定向
    • 再编辑一次,我混淆了错误的事件标签名称。这是fos_user.registration.confirm
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-30
    • 1970-01-01
    • 2013-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多