Symfony事件监听器
我创建了一些新事件,如Symfony事件监听器,symfony,events,event-listener,symfony-3.3,Symfony,Events,Event Listener,Symfony 3.3,我创建了一些新事件,如app.client\u enter或app.client\u leave。现在我想注册一个侦听器来侦听此事件。如果我在同一个命令中添加一个侦听器,它就可以工作了 ClientListener.php service.yml(更新) ClientCommand.php 它是标签的name:kernel.event\u listener谢谢大家。我在ContainerWareCommand中找到了解决方案,您必须使用event_dispatcher的服务 ClientComm
app.client\u enter
或app.client\u leave
。现在我想注册一个侦听器来侦听此事件。如果我在同一个命令中添加一个侦听器,它就可以工作了
ClientListener.php
service.yml(更新)
ClientCommand.php
它是标签的
name:kernel.event\u listener
谢谢大家。我在ContainerWareCommand中找到了解决方案,您必须使用event_dispatcher的服务
ClientCommand.php
使用此服务后,我的事件会触发侦听器。只是一个提示ẃ 让这一切变得更好 由于Symfony 3.3+中的大量依赖项注入发生了变化,您可以将许多容易出错的代码委托给Symfony
简化服务注册 它不适用于侦听器,因为有额外的标记,但它适用于订阅者-我建议使用它们来防止任何额外的冗余配置编程
以干净的方式获取参数-通过构造函数 使用它,您可以在命令中使用构造函数注入
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更改的更多信息,请参阅。您的标记错误。仔细看看这个例子:name:kernel.event\u listeneryes event\u dispatcher是管理通知的事件的通用容器,或者正如文档所说的“dispatcher是中心对象”。因此,当实例化一个新的EventDispatcher时,将不会有任何侦听器连接到它。
services:
app.client_listener:
class: AppBundle\Service\ClientListener
tags:
- { name: kernel.event_listener, event: app.client_enter, method: onClientEnter }
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"));
}
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"));
}
# 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(...);
}
}