【问题标题】:Create multidimensional array with symfony configuration tree builder使用 symfony 配置树构建器创建多维数组
【发布时间】:2015-08-06 21:05:53
【问题描述】:

我想为我自己的包创建一个配置,如下所示:

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');
    }
}

我看不到我的错误,谁能告诉我?

【问题讨论】:

  • 你的rootNode 是什么?更改您的示例之一以相互遵守。现在您已经定义了filter_expressions,但提供了my_filtersfilter_type_a。目前还不清楚。
  • 抱歉,忘记了。我已将根节点添加到示例中。
  • 我认为你的命名错误。你用同一个名称声明两个不同的节点my_filters
  • 即使我更改了内部标量节点的名称,也会出现错误。

标签: php symfony multidimensional-array configuration


【解决方案1】:

匹配配置

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'是空的,如果我尝试用$container->getParameter('my_bundle.my_filters')读取它。
  • 好的。在您设置参数my_bundle.my_filters 的位置发布您的代码。我认为这个问题会存在。
  • 贴出全班代码。你确定你的loadConfig在容器编译时被调用了吗?
  • 完成。是的,我很确定。
  • 您清除缓存了吗?如果是这样尝试手动删除app/cache/下的所有文件
猜你喜欢
  • 1970-01-01
  • 2014-05-27
  • 1970-01-01
  • 2018-05-30
  • 1970-01-01
  • 2023-03-29
  • 2021-09-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多