在您提供$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;
}
最好的!