簡體   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