我在 Symfony2 中有一个命令,我希望能够从中读取开发配置config_dev.yml
我正在尝试在命令execute
方法中检索配置,如下所示:
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->getContainer()->getParameter('parse');
}
但是当我执行命令时:
$ php app/console acme:test
它给了我以下错误消息:
没有扩展能够加载“acme”的配置
config_dev.yml
我要检索的数据:
acme:
foo: "bar"
该命令扩展containerAwareCommand
:
class AcmeCommand extends containerAwareCommand
任何建议如何实现这一目标?
更新
配置树示例:
// src/Acme/ApiBundle/DependencyInjection/Configuration.php
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('acme');
$rootNode
->children()
->scalarNode('foo')->end()
->end()
;
return $treeBuilder;
}
扩展示例:
// src/Acme/ApiBundle/DependencyInjection/AcmeApiExtension.php
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$container->setParameter('acme', $config);
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');
config_dev.yml 示例:
...
acme:
foo: "bar"