Php 在Symfony2 ContainerWareCommand中获取服务定义

Php 在Symfony2 ContainerWareCommand中获取服务定义,php,symfony,dependency-injection,Php,Symfony,Dependency Injection,我正在尝试获取ContainerWareCommand中的服务定义,如 但是,这会立即导致故障: 致命错误:调用未定义的方法AppDevDebBugProjectContainer::getDefinition() 我在文档中找不到更多关于这种行为的信息,有什么想法吗 编辑:代码示例: class MyCommand extends ContainerAwareCommand { protected function execute(InputInterface $p_vInput,

我正在尝试获取ContainerWareCommand中的服务定义,如

但是,这会立即导致故障:

致命错误:调用未定义的方法AppDevDebBugProjectContainer::getDefinition()

我在文档中找不到更多关于这种行为的信息,有什么想法吗

编辑:代码示例:

class MyCommand extends ContainerAwareCommand {

    protected function execute(InputInterface $p_vInput, OutputInterface $p_vOutput) {
        try {
            var_dump($this->getContainer()->getDefinition('api.driver'));
        } catch (\Exception $e) {
            print_r($e);
            exit;
        }
    }

}

在您提供的示例中,
$container
不是
container
类的实例,而是
ContainerBuilder
类的实例。容器没有任何名为
getDefinition()
的方法

如果你不展示你想要使用这个定义的上下文,我就说不出更多了

编辑:

下面我发布了使用
ContainerBuilder
的代码示例。它是直接从symfony的命令复制的,所以我想这是一个很好的使用示例

// Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php

/**
 * Loads the ContainerBuilder from the cache.
 *
 * @return ContainerBuilder
 */
private function getContainerBuilder()
{
    if (!$this->getApplication()->getKernel()->isDebug()) {
        throw new \LogicException(sprintf('Debug information about the container is only available in debug mode.'));
    }

    if (!file_exists($cachedFile = $this->getContainer()->getParameter('debug.container.dump'))) {
        throw new \LogicException(sprintf('Debug information about the container could not be found. Please clear the cache and try again.'));
    }

    $container = new ContainerBuilder();

    $loader = new XmlFileLoader($container, new FileLocator());
    $loader->load($cachedFile);

    return $container;
}

最好的

拜托,你能提供你写的一堆代码吗?很好的答案,工作如期。您能详细说明一下为什么它返回的内容不同于ContainerWareCommand->getContainer()?我不确定您是否理解上面的问题,但Container类不同于ContainerBuilder类。ContainerWareCommand->getContainer()返回类容器的对象。你需要ContainerBuilder,所以你必须像我回答的那样。好吧,我的印象是容器也有能力得到定义!