【问题标题】:Symfony2 yaml array optionsSymfony2 yaml 数组选项
【发布时间】:2012-06-04 20:52:49
【问题描述】:

app/config/config.yml 中,我为我的捆绑包添加了一些自定义设置

acme:
    acme_services:    
      service_a:
        options: { name: I, id: X, type: F, error: E }
      service_b:
        options: { name: J, id: Z, type: F, error: E }

现在在src/ACME/Bundle/ACMEBundle/DependencyInjection/Configuration.php 我如何设置默认值和/ 或检查service_a/service_b

public function getConfigTreeBuilder()
{
    $treeBuilder = new TreeBuilder();
    $rootNode = $treeBuilder->root('acme');

    $rootNode
        ->children()
            // also removed the ->end() for each arrayNode but then I get a Fatal Error 
            ->arrayNode('acme_services')->end()
            ->arrayNode('another')->end()
            ->arrayNode('more')->end()
            ->arrayNode('blah')->end()
        ->end();

    return $treeBuilder;
}

所以我需要拉出service_aservice_b 数组,但是service_aservice_b 出现Unrecognized options 错误。

想要的结果是我想在acme_services 数组中同时拥有service_aservice_b,这就是为什么我可以针对acme_services 数组验证使用的任何服务,service_a 或@ 987654338@.

注意:在 PHP 中我会这样写:(不确定这是否正确,但这是一个示例)

$acme_services = array(
    'acme_services' =>
        'service_a' => array(
            'options' => array(
                'name' => 'I',
                'id'   => 'X',
                'type' => 'F',
                'error'=> 'E',
            )
        ),
        'service_b' => array(
            'options' => array(
                'name' => 'J',
                'id'   => 'Z',
                'type' => 'F',
                'error'=> 'E',
            )
        )
);

【问题讨论】:

    标签: php dependency-injection symfony yaml


    【解决方案1】:

    你想要使用的是 Prototypes,类似于:

    $rootNode
        ->children()
           ->prototype('array')
              ->children()
                  ->arrayNode('options')
                      ->children()
                      ->scalarNode('name')->end()
                      ->scalarNode('id')->end()
                      ->scalarNode('type')->end()
                      ->scalarNode('error')->end()
                  ->end()
               ->end()
         ->end()
    ->end()
    

    这样,只要遵循此模式,您就可以定义任意数量的服务。

    【讨论】:

      【解决方案2】:

      你可以试试:

      public function getConfigTreeBuilder()
      {
          $treeBuilder = new TreeBuilder();
          $rootNode = $treeBuilder->root('acme');
      
          $rootNode
              ->children()
                  // also removed the ->end() for each arrayNode but then I get a Fatal Error 
                  ->arrayNode('acme_services')
                       ->children()
                            ->arrayNode('service_a')
                                ->children()
                                    ->arrayNode('options')->end()
                            ->arrayNode('service_b')
                                ->children()
                                    ->arrayNode('options')->end()
                 ->end();
      
          return $treeBuilder;
      }
      

      【讨论】:

      • 但是 service_b 呢?我还需要单独为此编码吗?或者我可以将 service_a 和 service_b 都拉到 amce_services 数组中吗?
      【解决方案3】:

      试试这个:

      $rootNode
         ->children()
              ->arrayNode('acme_services')
                  ->prototype('array')
                      ->children()
                          ->arrayNode('options')
                              ->children()
                                  ->scalarNode('name')->end()
                                  ->scalarNode('id')->end()
                                  ->scalarNode('type')->end()
                                  ->scalarNode('error')->end()
                              ->end()
                          ->end()
                      ->end()
                  ->end()
              ->end()
          ->end()
      ->end();
      

      【讨论】:

        猜你喜欢
        • 2013-03-10
        • 2015-09-19
        • 2015-03-08
        • 2012-12-29
        • 2012-07-06
        • 1970-01-01
        • 1970-01-01
        • 2012-05-08
        • 1970-01-01
        相关资源
        最近更新 更多