我想为我自己的包创建一个配置,如下所示:
my_filters:
filter_type_a:
- \MyBundle\My\ClassA
- \MyBundle\My\ClassA2
filter_type_b:
- \MyBundle\My\ClassB
my_filters
应该是一个可变长度的数组,my_filters.filter_type_a
也应该是一个可变长度的数组。
我试过
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('my_bundle');
$rootNode
->children()
->arrayNode('my_filters')
->prototype('array')
->prototype('array')
->children()
->scalarNode('my_filters')->end()
->end()
->end()
->end()
->end()
->end()
但我收到以下错误:Invalid type for path "my_bundle.my_filters.filter_type_a.0". Expected array, but got string
。
这是我设置配置的地方:
class MyBundleExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$this->loadServices($container);
$this->loadConfig($configs, $container);
}
private function loadConfig(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$container->setParameter('my_bundle.my_filters', $config['my_filters']);
}
private function loadServices(ContainerBuilder $container)
{
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
}
}
我看不出自己的错误,有人能告诉我吗?
答案 0 :(得分:2)
匹配配置
my_bundle:
my_filters:
filter_type_a:
- \MyBundle\My\ClassA
- \MyBundle\My\ClassA2
filter_type_b:
- \MyBundle\My\ClassB
您需要在配置树构建器中使用下一个代码:
$rootNode = $treeBuilder->root('my_bundle');
$rootNode
->children()
->arrayNode('my_filters')
->prototype('array')
->prototype('scalar')->end()
->end()
->end()
->end()