[英]Symfony 4: how to allow dynamic values for array nodes in bundle configuration?
In the Configuration
of my bundle I have defined the following for example:在我的捆绑包的
Configuration
中,我定义了以下示例:
$treeBuilder = new TreeBuilder('foo');
$treeBuilder
->getRootNode()
->children()
->arrayNode('foobar')
->scalarPrototype()->end()
->defaultValue([])
->end()
->end()
;
ie, the bundle expects an array of values in foo.foobar
.即,捆绑包需要
foo.foobar
中的值数组。 Now, in the case of my bundle, these values are dependent on the environment the application is run in. Thus it would make sense to provide this configuration value through an environment variable.现在,在我的包的情况下,这些值取决于运行应用程序的环境。因此,通过环境变量提供此配置值是有意义的。 Since Symfony 3.4 values of environment variables can be processed.
由于 Symfony 3.4可以处理环境变量的值。 So this array could be stored JSON encoded in the environment variable and then be decoded via
%env(json:FOO)%
.所以这个数组可以存储在环境变量中编码的 JSON ,然后通过
%env(json:FOO)%
解码。 So instead of having to write所以不必写
foo:
foobar:
- Lorem
- Ipsum
- Dolor
one could instead use可以改为使用
foo:
foobar: '%env(json:FOO)%'
where在哪里
FOO=["Lorem","Ipsum","Dolor"]
However, when trying to do that Symfony throws the following exception:但是,当尝试这样做时,Symfony 会引发以下异常:
A dynamic value is not compatible with a "Symfony\Component\Config\Definition\PrototypedArrayNode" node type at path "foo.foobar".
What is the correct way of allowing such dynamic values for array nodes?允许数组节点使用此类动态值的正确方法是什么? Do I need to implement my own normalization in the configuration tree for example?
例如,我是否需要在配置树中实现自己的规范化?
you need to define the JSON in the.env file as string您需要将 .env 文件中的 JSON 定义为字符串
this is correct: FOO='["Lorem","Ipsum","Dolor"]'
这是正确的:
FOO='["Lorem","Ipsum","Dolor"]'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.