我的 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()
andcanBeDisabled()
methods.
关于php - Symfony 配置 : Array node or null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28590725/