[英]symfony EventSubscriber ignored in Symfony 3.4
I'm trying to use an event subscriber to redirect person registering to a different route, than the standard FOSUser bundle directs them to.我正在尝试使用事件订阅者将注册的人重定向到不同的路线,而不是标准 FOSUser 捆绑包将他们定向到的路线。 Going by some tutorials for Symfony 3.3, but wondered as I have version 3.4 if anything needs changing to make it work, as it currently just goes to the standard page?
浏览 Symfony 3.3 的一些教程,但想知道我有 3.4 版是否需要更改才能使其正常工作,因为它目前只是进入标准页面? Thanks
谢谢
EventSubscriber事件订阅者
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服务.yml
services:
_defaults:
autowire: true
autoconfigure: true
eventsBundle\:
resource: '../../src/eventsBundle/*'
exclude: '../../src/eventsBundle/{Entity,Repository,Tests}'
eventsBundle\EventListener\RedirectAfterRegistrationSubscriber:
autowire: true
I added die();我添加了 die(); just to make sure it was going to this but, has not effect
只是为了确保它会这样做但是,没有效果
The services.yml file to change is the one in my bundle not the main services.yml under app/config, this now picks up the EventSubscriber要更改的 services.yml 文件是我的包中的文件,而不是 app/config 下的主要 services.yml,现在它会选择 EventSubscriber
Looks like you need to add tag for your subscriber.看起来您需要为订阅者添加标签。
eventsBundle\EventListener\RedirectAfterRegistrationSubscriber:
autowire: true
tags:
- { name: kernel.event_subscriber }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.