Symfony2 FOSuserBundle 事件 REGISTRATION_COMPLETED 未触发
Symfony2 FOSuserBundle event REGISTRATION_COMPLETED is not triggered
对于我的项目,我需要在注册后重定向用户。为了实现这一点,我创建了一个 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
您的侦听器声明它已订阅 FOSUserEvents::REGISTRATION_CONFIRM
,但您正在调度 FOSUserEvents::REGISTRATION_COMPLETED
。要触发它,您需要发送一个 FOSUserEvents::REGISTRATION_CONFIRM
事件
编辑以匹配您的编辑,您需要在您的服务中传递事件 tags
:
- { name: 'kernel.event_subscriber', event: 'fos_user.registration.confirm'}
对于我的项目,我需要在注册后重定向用户。为了实现这一点,我创建了一个 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
您的侦听器声明它已订阅 FOSUserEvents::REGISTRATION_CONFIRM
,但您正在调度 FOSUserEvents::REGISTRATION_COMPLETED
。要触发它,您需要发送一个 FOSUserEvents::REGISTRATION_CONFIRM
事件
编辑以匹配您的编辑,您需要在您的服务中传递事件 tags
:
- { name: 'kernel.event_subscriber', event: 'fos_user.registration.confirm'}