【问题标题】:symfony EventSubscriber ignored in Symfony 3.4symfony EventSubscriber 在 Symfony 3.4 中被忽略
【发布时间】: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

标签: php symfony events


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 相当肯定标签从 3.4 版开始不需要,如果你有 - autowire: true autoconfigure: true, symfony.com/doc/3.4/…。试过了,还是没解决问题
    猜你喜欢
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 2011-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-07
    • 1970-01-01
    相关资源
    最近更新 更多