2

我正在尝试根据http://symfony.com/doc/2.0/components/dependency_injection/definitions.html#getting-and-setting-service-definitions在 ContainerAwareCommand 中获取服务定义

但是,这会立即导致失败:

致命错误:调用未定义的方法 appDevDebugProjectContainer::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;
        }
    }

}
4

1 回答 1

3

在您提供$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;
}

最好的!

于 2012-10-17T12:12:21.167 回答