在服务中注入事件调度器在生产环境中不起作用 - symfony 3.4



假设我有一个侦听器作为这样的服务

class MyListener
{
/**
* @var SymfonyComponentEventDispatcherContainerAwareEventDispatcher 
*/
private $dispatcher;
function __construct(ContainerAwareEventDispatcher $dispatcher)
{
$this->dispatcher = $dispatcher;
}
public function onKernelRequest(GetResponseEvent $event)
{
//in my logic here I use $this->dispatcher->dispatch(my-own-event)
}
}

服务.yml

ap.my_listener:
class: my-name-spaceMyListener
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
arguments: ["@debug.event_dispatcher.inner"]

它在开发环境中工作正常,但是当我在清除缓存后切换到生产环境时,我收到以下异常:The service "ap.my_listener" has a dependency on a non-existent service "debug.event_dispatcher.inner"

当我按时按下时,我只是注入了所有容器,所以到底发生了什么,还有另一种方法可以注入事件调度程序而无需从容器中检索它

你应该注入事件调度程序并使用类似

use SymfonyComponentEventDispatcherEventDispatcherInterface;
class MyListener
{
/**
* @var SymfonyComponentEventDispatcherEventDispatcherInterface 
*/
private $dispatcher;
function __construct(EventDispatcherInterface $dispatcher)
{
$this->dispatcher = $dispatcher;
}
public function onKernelRequest(GetResponseEvent $event)
{
//in my logic here I use $this->dispatcher->dispatch(my-own-event)
}
}
//service.yml
ap.my_listener:
class: my-name-spaceMyListener
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
arguments: ["@event_dispatcher"]

相关内容

  • 没有找到相关文章

最新更新