我在Symfony2中有一个命令,我希望能够从config_dev.yml
读取devel配置
我试图在命令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"
如果您只是对从配置文件中存储和检索任意数据感兴趣,那么您应该将其存储在parameters.yml
中,而不是config_dev.yml
中。
parameters.yml
中的配置条目可以是自由形式的,只要它们位于parameters:
键之下即可,而config*.yml
文件中的实体需要符合捆绑包配置中定义的结构。
但是,您应该首先为要存储在parameters.yml.dist
中的数据定义密钥。
您是否像这里一样定义了配置树?