繁体   English   中英

symfony EventSubscriber 在 Symfony 3.4 中被忽略

[英]symfony EventSubscriber ignored in Symfony 3.4

我正在尝试使用事件订阅者将注册的人重定向到不同的路线,而不是标准 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]
    ];
   }
  }

服务.yml

 services:
         _defaults:
         autowire: true
         autoconfigure: true        

eventsBundle\:
    resource: '../../src/eventsBundle/*'
    exclude: '../../src/eventsBundle/{Entity,Repository,Tests}'

eventsBundle\EventListener\RedirectAfterRegistrationSubscriber:
    autowire: true

我添加了 die(); 只是为了确保它会这样做但是,没有效果

要更改的 services.yml 文件是我的包中的文件,而不是 app/config 下的主要 services.yml,现在它会选择 EventSubscriber

看起来您需要为订阅者添加标签。

eventsBundle\EventListener\RedirectAfterRegistrationSubscriber:
    autowire: true 
    tags:
        - { name: kernel.event_subscriber }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM