douli4337 2019-07-11 23:56
浏览 165
已采纳

无法读取环境变量

I have a simple Symfony project, using "symfony/dotenv": "4.3.*", in the composer.json and trying to read in the value of an environment variable.

This is my command:

var_dump($_ENV['MY_NEW_VAR']);

This is my .env file

MY_NEW_VAR=testing

Upon executing the command I get the following

string(7) "testing"

All good so far.... But now if I want to use environment variables to override the setting it returns an error. First I set the environment variable

export MY_NEW_VAR="something_else"

Then execute the command again, with the following results

[ErrorException]
Notice: Undefined index: MY_NEW_VAR

If I remove the environment variable using unset MY_NEW_VAR and execute the command again I get the value from the .env file

I would expect the environment variable to override the .env setting as explained in the comments of the .env file

# 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.

Why does it not work with environment variables? Am I missing something?

  • 写回答

1条回答 默认 最新

  • duande8497 2019-07-12 00:34
    关注

    The code in your question is correct, so I'd wager the problem lies somewhere else.

    There are many vagaries and inconsistencies about how getenv(), $_ENV and $_SERVER work. I have found $_SERVER to be more reliable in my experience, and many times it safer to check both. E.g. if you check the code on config/bootstrap.php for Symfony 4.3 you'll see stuff like this:

    $_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = ($_SERVER['APP_ENV'] ?? $_ENV['APP_ENV'] ?? null) ?: 'dev';
    

    In any case, if your $_ENV superglobal is initially empty, it could be because on the variables_order setting. Check its value, and if doesn't include an E, prepend it to the existing value and with that it should be populated correctly.

    The docs say that S is equivalent to ES...

    In both the CGI and FastCGI SAPIs, $_SERVER is also populated by values from the environment; S is always equivalent to ES regardless of the placement of E elsewhere in this directive.

    but I have not found it to be consistently so.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 robocopy文件复制
  • ¥15 unity安卓打包出现问题
  • ¥15 爱快路由器端口更改错误导致无法访问
  • ¥20 安装catkin时遇到了如下问题请问该如何解决呢
  • ¥15 VAE模型如何输出结果
  • ¥15 编译python程序为pyd文件报错:{"source code string cannot contain null bytes"
  • ¥20 关于#r语言#的问题:广义加行模型拟合曲线后如何求拐点
  • ¥15 fluent设置了自动保存后,会有几个时间点不保存
  • ¥20 激光照射到四象线探测器,通过液晶屏显示X、Y值
  • ¥50 数据库开发问题求解答
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部