【问题标题】:Attempted to call an undefined method named "arrayNode"试图调用名为“arrayNode”的未定义方法
【发布时间】:2017-10-22 22:05:54
【问题描述】:

我在 Symfony 3.5 项目的 config.yml 文件中有这个配置:

my_config:
    token: mHSHlSHl-QqSHlX-SHlQqShzO2ibzGnsNk-Q
    username: test

    development:
        developers_id: [130]
        maintenance:
            enable: true
            text: "text of text"

我有一个包来解析我的configuration.php 文件中的这个配置。在捆绑依赖注入中,我有以下代码:

$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('my_bundle');
$rootNode->children()
    ->scalarNode("username")
    ->end()
    ->scalarNode("token")
        ->isRequired()
    ->end()
    ->arrayNode('development')
        ->children()
            ->booleanNode('send_log')
                ->defaultFalse()
            ->end()
            ->arrayNode('developers_id')
                ->prototype('scalar')
            ->end()
            ->arrayNode('maintenance')
                ->children()
                    ->booleanNode('enable')
                        ->defaultFalse()
                    ->end()
                    ->scalarNode('text')
                        ->defaultValue('default text')
                    ->end()
                ->end()
            ->end()
        ->end()
    ->end()
->end();

return $treeBuilder;

当我运行这段代码时,我得到了这个错误:

试图调用类的名为“arrayNode”的未定义方法 "Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition"。

我该怎么办?

【问题讨论】:

  • 尝试清除缓存 (cache:clear [--env=...] [--no-debug]),如果这没有帮助:在引发错误的行之前分解代码并在此时转储 $rootNode 以查看它的类型。
  • @ccKep 我无法运行命令,因为我收到错误
  • 你运行的只是使用 rm -rf var/cache 删除缓存

标签: php symfony configuration


【解决方案1】:

我收到这个错误是因为在->prototype('scalar') 之后我没有写->end(),我添加了->end() 并且我的问题解决了

正确的语法是这样的:

->arrayNode('developers_id')    
    ->prototype('scalar')
    ->end()
->end()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-27
    • 2015-12-20
    • 2018-12-15
    相关资源
    最近更新 更多