在app/config/config.yml
我已为我的论坛添加了一些自定义设置
acme:
acme_services:
service_a:
options: { name: I, id: X, type: F, error: E }
service_b:
options: { name: J, id: Z, type: F, error: E }
现在在src/ACME/Bundle/ACMEBundle/DependencyInjection/Configuration.php
如何设置默认值/
或检查service_a
/ service_b
?
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('acme');
$rootNode
->children()
// also removed the ->end() for each arrayNode but then I get a Fatal Error
->arrayNode('acme_services')->end()
->arrayNode('another')->end()
->arrayNode('more')->end()
->arrayNode('blah')->end()
->end();
return $treeBuilder;
}
所以我需要提取service_a
和service_b
数组,但Unrecognized options
和service_a
出现service_b
错误。
我想在service_a
数组中同时拥有service_b
和acme_services
,这就是为什么我可以针对任何服务验证acme_services
数组的原因使用service_a
或service_b
。
注意:在PHP中我会这样写:(不确定这是否正确,但这是一个例子)
$acme_services = array(
'acme_services' =>
'service_a' => array(
'options' => array(
'name' => 'I',
'id' => 'X',
'type' => 'F',
'error'=> 'E',
)
),
'service_b' => array(
'options' => array(
'name' => 'J',
'id' => 'Z',
'type' => 'F',
'error'=> 'E',
)
)
);
答案 0 :(得分:2)
您想要使用的是 Prototypes ,例如:
$rootNode
->children()
->prototype('array')
->children()
->arrayNode('options')
->children()
->scalarNode('name')->end()
->scalarNode('id')->end()
->scalarNode('type')->end()
->scalarNode('error')->end()
->end()
->end()
->end()
->end()
这样,只要遵循此模式,您就可以根据需要定义任意数量的服务。
答案 1 :(得分:1)
您可以尝试:
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('acme');
$rootNode
->children()
// also removed the ->end() for each arrayNode but then I get a Fatal Error
->arrayNode('acme_services')
->children()
->arrayNode('service_a')
->children()
->arrayNode('options')->end()
->arrayNode('service_b')
->children()
->arrayNode('options')->end()
->end();
return $treeBuilder;
}
答案 2 :(得分:0)
试试这个:
$rootNode
->children()
->arrayNode('acme_services')
->prototype('array')
->children()
->arrayNode('options')
->children()
->scalarNode('name')->end()
->scalarNode('id')->end()
->scalarNode('type')->end()
->scalarNode('error')->end()
->end()
->end()
->end()
->end()
->end()
->end()
->end();