gpt4 book ai didi

symfony - Symfony 中可为 null 的环境变量

转载 作者:行者123 更新时间:2023-12-02 16:51:54 27 4
gpt4 key购买 nike

我知道环境变量只是字符串。 Symfony 3.4 supports env variables type casting 。有没有办法通过 int 验证器传递 null 值?

.env

DATABASE_PORT=
#I also tried DATABASE_PORT=null, DATABASE_PORT=~

参数.yml

app.connection.port: '%env(int:DATABASE_PORT)%'
#I also tried env(?int, env(int?

我收到错误:“非数字环境变量“DATABASE_PORT”无法转换为 int。”或“无效的 env(?int:DATABASE_PORT) 名称:仅允许使用“word”字符。”

在 yml 中,有 ~ 或 null 符号用于传递 null,例如:

app.connection.port: ~

最佳答案

有人在文档中问:

Is it, or will it be possible to define your own operator? Like %env(myDecoder:API_PASSWORD)%, which will decrypt the environment value into something usable.

class MyDecoder implements EnvironmentOperator {
public function resolve(string $value): string {
// magic stuff for decryption
return $value;
}
}

答案是:

yes, you just need to create a service that implements EnvProviderInterface and tag it with container.env_provider.

您可以创建一个运算符来接受 null 作为 int

关于symfony - Symfony 中可为 null 的环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49646396/

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