代码之家  ›  专栏  ›  技术社区  ›  Y4roc

Symfony事件侦听器

  •  2
  • Y4roc  · 技术社区  · 8 年前

    我创造了一些新的活动,比如 app.client_enter app.client_leave . 现在我想注册一个监听器来监听这个事件。如果我在同一个命令中添加一个侦听器,它就会工作。

    ClientListener。php

    namespace AppBundle\Service;
    use AppBundle\Event\ClientEnterEvent;
    
    class ClientListener {
    
      public function onClientEnter(ClientEnterEvent $event) {
        echo "It could be working";
      }
    
    }
    

    服务yml公司 (更新)

    services:
      app.client_listener:
        class: AppBundle\Service\ClientListener
        tags:
          - { name: kernel.event_listener, event: app.client_enter, method: onClientEnter }
    

    ClientCommand。php

    namespace AppBundle\Command;
    
    use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
    use Symfony\Component\Console\Input\InputInterface;
    use Symfony\Component\Console\Output\OutputInterface;
    use Symfony\Component\EventDispatcher\EventDispatcher;
    use AppBundle\Event\ClientEnterEvent;
    
    class ClientCommand extends ContainerAwareCommand {
      protected function configure() { ... }
      protected function execute(InputInterface $input, OutputInterface $output) {
        $dispatcher = new EventDispatcher();
        $dispatcher->dispatch('app.client_enter', new ClientEnterEvent("Maxi"));
    }
    
    3 回复  |  直到 8 年前
        1
  •  3
  •   mblaettermann    8 年前

    它是 name: kernel.event_listener 对于标签

        2
  •  3
  •   Y4roc    8 年前

    ClientCommand。php

    namespace AppBundle\Command;
    
    use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
    use Symfony\Component\Console\Input\InputInterface;
    use Symfony\Component\Console\Output\OutputInterface;
    use Symfony\Component\EventDispatcher\EventDispatcher;
    use AppBundle\Event\ClientEnterEvent;
    
    class ClientCommand extends ContainerAwareCommand {
      protected function configure() { ... }
      protected function execute(InputInterface $input, OutputInterface $output) {
        $dispatcher = $this->getContainer->get('event_dispatcher');
        $dispatcher->dispatch('app.client_enter', new ClientEnterEvent("Maxi"));
    }
    

        3
  •  1
  •   Tomas Votruba    8 年前

    给你一个小提示,让这一切变得更好。

    由于大量依赖注入在 Symfony 3.3+ 您可以将许多容易出错的代码委托给Symfony。


    简化服务注册

    # app/config/services.yml
    
    services:
        _defaults:
            autowire: true
    
        AppBundle\:
            resouce: '../../src/AppBundle'
    

    它不适用于侦听器,因为有额外的标记,但它适用于订阅者-我建议使用它们来防止任何额外的冗余配置编程。


    以干净的方式获取参数-通过构造函数

    使用它,您可以在现成的命令中使用构造函数注入。

    use Symfony\Component\EventDispatcher\EventDispatcherInterface
    
    class ClientCommand extends Command
    {
        /**
         * @var EventDispatcherInterface
         */
        private $eventDispatcher;
    
        public function __construct(EventDispatcherInterface $eventDispatcher)
        {
            $this->eventDispatcher = $eventDispatcher;
        }
    
        protected function execute(InputInterface $input, OutputInterface $output)
        {
            $this->eventDispatcher->dispatch(...);
        }  
    }
    

    要了解有关DI更改的更多信息,请参阅 this post with before/after examples