使用 symfony 配置树生成器创建多维数组
Create multidimensional array with symfony configuration tree builder
我想像这样为我自己的包创建一个配置:
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');
}
}
我看不出我的错误,谁能告诉我?
匹配配置
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()
我想像这样为我自己的包创建一个配置:
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');
}
}
我看不出我的错误,谁能告诉我?
匹配配置
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()