我想像这样为我自己的包创建一个配置:
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()
但我收到以下错误:路径“my_bundle.my_filters.filter_type_a.0”的类型无效。预期的数组,但得到的是字符串
。
这里是我设置配置的地方:
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');
}
}
我看不出我的错误,谁能告诉我?
最佳答案
匹配配置
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()
关于php - 使用 symfony 配置树生成器创建多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30437401/