Php 在向eventListener(FOSUserBundle)注册后尝试重定向用户时出错
现在,我正在使用FOSUserBundle和eventListener注册后进行重定向 RegistrationConfirmListener.phpPhp 在向eventListener(FOSUserBundle)注册后尝试重定向用户时出错,php,symfony,redirect,fosuserbundle,event-listener,Php,Symfony,Redirect,Fosuserbundle,Event Listener,现在,我正在使用FOSUserBundle和eventListener注册后进行重定向 RegistrationConfirmListener.php <?php namespace BISSAP\UserBundle\EventListener; use FOS\UserBundle\FOSUserEvents; use FOS\UserBundle\Event\UserEvent; use Symfony\Component\EventDispatcher\EventSubscri
<?php
namespace BISSAP\UserBundle\EventListener;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\UserEvent;
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_SUCCESS => 'onRegistrationSuccess'
);
}
public function onRegistrationSuccess(GetResponseUserEvent $event)
{
$url = $this->router->generate('bissap_bodyconcept_homepage');
$event->setResponse(new RedirectResponse($url));
}
}
我试着移动
public function onRegistrationSuccess(GetResponseUserEvent $event)
借
但我得到:
Catchable Fatal Error: Argument 1 passed to BISSAP\UserBundle\EventListener\RegistrationConfirmListener::onRegistrationSuccess() must be an instance of BISSAP\UserBundle\EventListener\FOS\UserBundle\Event\FormEvent, instance of FOS\UserBundle\Event\FormEvent given
当我将registrationconfirmstener.php中的REGISTRATION\u SUCCESS
替换为REGISTRATION\u confirmstener
时:
我没有收到任何错误,但重定向不起作用
信息:我使用的是FOSUserBundle中最初的RegistrationController.php,您错过了第一个反斜杠。它是注册成功(\FOS\UserBundle\Event\FormEvent$Event)的公共函数。是的,您需要初始的“\”将其带到全局命名空间,否则它将在当前命名空间中查找类/命名空间。或者,您可以添加
使用FOS\UserBundle\Event\FormEvent
,然后在方法中添加公共函数onRegistrationSuccess(FormEvent$Event)
。
public function onRegistrationSuccess(FOS\UserBundle\Event\FormEvent $event)
Catchable Fatal Error: Argument 1 passed to BISSAP\UserBundle\EventListener\RegistrationConfirmListener::onRegistrationSuccess() must be an instance of BISSAP\UserBundle\EventListener\FOS\UserBundle\Event\FormEvent, instance of FOS\UserBundle\Event\FormEvent given
namespace BISSAP\UserBundle\EventListener;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\UserEvent;
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('bissap_bodyconcept_homepage');
$event->setResponse(new RedirectResponse($url));
}
}