我有一个简单的 Symfony 项目,使用"symfony/dotenv": "4.3.*",
并composer.json
尝试读取环境变量的值。
这是我的命令:
var_dump($_ENV['MY_NEW_VAR']);
这是我的.env
文件
MY_NEW_VAR=testing
执行命令后,我得到以下信息
string(7) "testing"
到目前为止一切都很好......但是现在如果我想使用环境变量来覆盖设置,它会返回一个错误。首先我设置环境变量
export MY_NEW_VAR="something_else"
然后再次执行命令,结果如下
[ErrorException]
Notice: Undefined index: MY_NEW_VAR
如果我使用删除环境变量unset MY_NEW_VAR
并再次执行命令,我会从.env
文件中获取值
我希望环境变量能够覆盖文件.env
注释中解释的设置.env
# In all environments, the following files are loaded if they exist,
# the later taking precedence over the former:
#
# * .env contains default values for the environment variables needed by the app
# * .env.local uncommitted file with local overrides
# * .env.$APP_ENV committed environment-specific defaults
# * .env.$APP_ENV.local uncommitted environment-specific overrides
#
# Real environment variables win over .env files.
为什么它不适用于环境变量?我错过了什么吗?