Symfony 2.5.6错误InvalidArgumentException:服务定义"event_dispatcher"不存在

Aph*_*hax 0 symfony

我正在尝试在Symfony 2中构建我的第一个Compiler Pass.现在,我只是想在SampleBundle中从FrameWorkBundle获取核心event_dispatcher服务,但是我收到了这个错误:

error InvalidArgumentException: The service definition "event_dispatcher" does not exist.
Run Code Online (Sandbox Code Playgroud)

这是我的编译器的代码:

<?php
namespace Me\SampleBunlde\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;

class RegisterListenersPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
        $definition = $container->getDefinition('event_dispatcher');
    }
}
?>
Run Code Online (Sandbox Code Playgroud)

我有点惊讶,因为我一步一步地遵循一本专业的Symfony书,向我保证我会找到这个服务.

我已经做了一些关于这方面的研究,我发现只有debug.event_dispatcher服务是可用的.然后我检查了别名,发现有一个名为'event_dispatcher'的私有Alias指向debug.event_dispatcher.所以我真的很困惑.而且我想知道:

  • 为什么Alias是私有的?我需要将他设为公开还是错误的方式?
  • 为什么Symfony不会自动解释我的event_dispatcher调用?

谢谢您的帮助 !

Wou*_*r J 6

findDefinition()而不是getDefinition().findDefinition也寻找别名.