[英]Load devel configuration using a command in Symfony2
I have a command in Symfony2 and I want to be able to read the devel config from config_dev.yml
我在Symfony2中有一个命令,我希望能够从
config_dev.yml
读取devel配置
I'm trying to retrieve the config in the command execute
method like this: 我正在尝试在命令
execute
方法中检索配置,如下所示:
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->getContainer()->getParameter('parse');
}
But when I execute the command: 但是当我执行命令时:
$ php app/console acme:test
It gives me the following error message: 它给我以下错误消息:
There is no extension able to load the configuration for "acme"
没有扩展程序可以加载“acme”的配置
Data in config_dev.yml
that I want to retrieve: 我要检索的
config_dev.yml
中的数据:
acme:
foo: "bar"
The command extends containerAwareCommand
: 该命令扩展了
containerAwareCommand
:
class AcmeCommand extends containerAwareCommand
Any suggestions how to achieve this? 任何建议如何实现这一目标?
Update 更新
Configuration tree example: 配置树示例:
// src/Acme/ApiBundle/DependencyInjection/Configuration.php
// src / Acme / ApiBundle / DependencyInjection / Configuration.php
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('acme');
$rootNode
->children()
->scalarNode('foo')->end()
->end()
;
return $treeBuilder;
}
Extension example: 扩展示例:
// src/Acme/ApiBundle/DependencyInjection/AcmeApiExtension.php
// 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 example: config_dev.yml示例:
...
acme:
foo: "bar"
你有没有这样定义配置树在这里 ?
If you're just interested in storing and retrieving arbitrary data from a config file, then you should store it in parameters.yml
, rather than config_dev.yml
. 如果您只对从配置文件中存储和检索任意数据感兴趣,那么您应该将它存储在
parameters.yml
,而不是config_dev.yml
。
Config entries in parameters.yml
are allowed to be free-form, as long as they are below the parameters:
key, whereas entires in config*.yml
files need to conform to a structure defined in your bundle configuration. parameters.yml
中的配置条目允许为自由格式,只要它们低于parameters:
key,而config*.yml
文件中的config*.yml
需要符合bundle配置中定义的结构。
You should define the keys for data you want to store in parameters.yml.dist
first, however. 但是,您应该首先在
parameters.yml.dist
定义要存储的数据的键。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.