gpt4 book ai didi

Symfony2 配置 rootNode 无法识别

转载 作者:行者123 更新时间:2023-12-02 11:05:59 24 4
gpt4 key购买 nike

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

我创建了一个带有 getConfigTreeBuilderConfiguration 类,如下所示:

/**
* {@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 生成包时的原始根名称。我已经对此进行了 grep 查找,并且只是 wa_ndisco 并且没有其他引用。

如果我删除我的包的配置部分和转储引用,我会得到:

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

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

如果我确实给了它它认为应该有wa_ndisco_rally_stats的名称,我也无法访问它,我也会收到该名称相同的错误。

最佳答案

首先你的配置是错误的:

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

应该是:

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

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

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

Here你可以阅读一些相关内容。

关于Symfony2 配置 rootNode 无法识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28906181/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com