【发布时间】:2019-10-23 14:42:20
【问题描述】:
我正在尝试使用事件订阅者将注册的人重定向到不同的路线,而不是标准 FOSUser 捆绑包将他们定向到的路线。浏览 Symfony 3.3 的一些教程,但想知道我有 3.4 版,是否需要进行任何更改才能使其正常工作,因为它目前只是进入标准页面?谢谢
事件订阅者
namespace eventsBundle\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Http\Util\TargetPathTrait;
use Symfony\Component\Routing\RouterInterface;
use FOS\UserBundle\Event\FormEvent;
use Symfony\Component\HttpFoundation\RedirectResponse;
use FOS\UserBundle\FOSUserEvents;
class RedirectAfterRegistrationSubscriber implements
EventSubscriberInterface
{
use TargetPathTrait;
private $router;
public function __construct(RouterInterface $router)
{
$this->router = $router;
}
public function onRegistrationSuccess(FormEvent $event)
{
die();
// main is your firewall's name
$url = $this->getTargetPath($event->getRequest()->getSession(),
'main');
if (!$url) {
$url = $this->router->generate('homepage');
}
$response = new RedirectResponse($url);
$event->setResponse($response);
}
public static function getSubscribedEvents()
{
return [
FOSUserEvents::REGISTRATION_SUCCESS =>
['onRegistrationSuccess',-5]
];
}
}
services.yml
services:
_defaults:
autowire: true
autoconfigure: true
eventsBundle\:
resource: '../../src/eventsBundle/*'
exclude: '../../src/eventsBundle/{Entity,Repository,Tests}'
eventsBundle\EventListener\RedirectAfterRegistrationSubscriber:
autowire: true
我添加了 die();只是为了确保它会这样做,但是没有效果
【问题讨论】:
-
你能显示
bin/console debug:container "eventsBundle\EventListener\RedirectAfterRegistrationSubscriber"的输出吗? -
没有找到匹配“eventsBundle\EventListener\RedirectAfterRegistrationSubscriber”的服务。
-
你确定你的
services.yml文件已经被处理了吗? -
这让我意识到要使用的 services.yml 是我捆绑包中的那个,而不是 app/config 下的主要 services.yml