php - Symfony 配置 : Array node or null

标签 php symfony configuration config

我的 bundle 的某些功能是可选的。为了配置它们,我想有以下两个选项。如果我想启用该功能:

foo:
  feature:
    mandatory1: 'bar'
    mandatory2: 'bar2'

但是,如果我决定不使用此特定功能,我想将 feature 设置为 null:

foo:
  feature: ~

我目前有以下代码:

// ...
    ->arrayNode('feature')
        ->defaultNull()
        ->children()
            ->scalarNode('mandatory1')
                ->isRequired()
                ->cannotBeEmpty()
                ->end()
            ->scalarNode('mandatory2')
                ->isRequired()
                ->cannotBeEmpty()
                ->end()
            ->end()
        ->end()

但是,defaultNull() 调用是不允许的。有什么办法可以实现这种行为吗?

最佳答案

其实,我自己找到了答案。复制自 Symfony docs :

If you have entire sections which are optional and can be enabled/disabled, you can take advantage of the shortcut canBeEnabled() and canBeDisabled() methods.

关于php - Symfony 配置 : Array node or null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28590725/

相关文章:

php - 如何安装 doctrine 2.2 ORM

forms - Symfony 2 表单 - 从表单类访问parameters.ini

php - 与数据库连接的 parameters.yml 和 config_(dev|stage|prod).yml 混淆

php - Symfony 设置扩展类的依赖注入(inject)

configuration - 具有相同标签的 L1 实体类型和 L3 实体类型

configuration - LDAP 联合用户 - 自动将电子邮件验证设置为 true

php - curl 错误 : Recv failure: Connection reset by peer - PHP Curl

php - 插入查询不插入没有错误信息

java - BufferedReader在读取html页面时忽略\r\n?

java - Spring Security Configuration @Order 不是唯一异常