php - 使用 symfony 配置树生成器创建多维数组

标签 php symfony multidimensional-array configuration

我想像这样为我自己的包创建一个配置:

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/

相关文章:

php - Symfony 运行提示 kernel.secret 参数未找到

javascript - 比较两个字符串并显示字符串中不匹配单词的数量

symfony - Twig 读取图像高度/宽度属性

php - 使用lumen发送电子邮件,无需在env文件中设置smtp凭证

symfony - Symfony2样式的电子邮件最佳做法

php - 将数组列表与数组中的下一个列表进行比较

c++ - 多维数组作为具有任意边界的类成员

javascript - 如何在javascript中向二维数组添加数据

php - 用于确定一个句子是否等同于另一个句子的 NLP 库?

php - 如何使用两个插入查询循环复选框中的两个值