Symfony2配置rootNode无法识别

时间:2015-03-06 19:28:07

标签: symfony configuration bundle config

我正在尝试定义我的捆绑包用户可用的配置参数,而我在识别根节点时遇到问题。

我创建了一个Configuration类,其中getConfigTreeBuilder看起来像这样:

/**
 * {@inheritdoc}
 */
public function getConfigTreeBuilder()
{
    $treeBuilder = new TreeBuilder();
    $rootNode = $treeBuilder->root('rally_stats');

    // Here you should define the parameters that are allowed to
    // configure your bundle. See the documentation linked above for
    // more information on that topic.

    $rootNode->children()->arrayNode('projects')
        ->prototype('scalar')->end();

    return $treeBuilder;
}

使用它,我希望能够在config.yml中创建一个看起来像这样的部分:

rally_stats:
    projects:
        - project1
        - project2

然后能够通过以下方式访问配置:

$this->getContainer()->getParameter('rally_stats.projects');

但是,在尝试加载该配置时,我收到此错误:

[Symfony\Component\Config\Exception\FileLoaderLoadException]
There is no extension able to load the configuration for "rally_stats" 
(in /Users/stu/Projects/rally-stats/app/config/config.yml). Looked for namespace "rally_stats", found "framework", "security", "
twig", "monolog", "swiftmailer", "assetic", "doctrine", "sensio_framework_extra", 
"wa_ndisco_rally_stats", "jms_di_extra", "jms_aop", "debug", "web_profiler", "sensio_distribution" 
in /Users/stu/Projects/rally-stats/app/config/config.yml 
(which is being imported from "/Users/stu/Projects/rally-stats/app/config/config_dev.yml").

最接近的是wa_ndisco_rally_stats,这是Symfony生成捆绑包时的原始根名称。我已经为此而努力了只是wa_ndisco并且没有其他任何引用。

如果我删除配置部分& dump-reference for my bundle,我明白了:

# Default configuration for "WANdiscoRallyStatsBundle"
rally_stats:
    projects:             []

为什么我不能使用我提供的节点名称添加配置?

如果我给它认为它应该有wa_ndisco_rally_stats的名称,我也无法访问它,我也得到同样的错误。

1 个答案:

答案 0 :(得分:2)

首先,您的配置错误:

$rootNode->children()->arrayNode('projects')
    ->prototype('scalar')->end();

应该是:

$rootNode
    ->children()
        ->arrayNode('projects')
            ->prototype('scalar')->end()
        ->end()
    ->end();

如果你想从参数中访问它,你必须转到你的bundle扩展类并设置它,例如:

    $container->setParameter('rally_stats.projects', $config['projects']);

Here你可以阅读有关它的内容。