duandai6373
2018-02-01 05:13
浏览 231
已采纳

未定义的变量:Laravel 5.3中的_ENV

I was testing my system with Laravel 5.3:

For past few weeks my system was working fine. Since last weekend, I encountered the same error TWICE as below:-

Notice: Undefined variable: _ENV in C:\NewFolder\htdocs\project\vendor\vlucas\phpdotenv\src\Loader.php on line 303

Warning: array_key_exists() expects parameter 2 to be array, null given in C:\NewFolder\htdocs\project\vendor\vlucas\phpdotenv\src\Loader.php on line 303

Loader.php:

public function getEnvironmentVariable($name)
{
    switch (true) {
        case array_key_exists($name, $_ENV): // line 303 here
            return $_ENV[$name];
        case array_key_exists($name, $_SERVER):
            return $_SERVER[$name];
        default:
            $value = getenv($name);
            return $value === false ? null : $value;
    }
}

.env

APP_ENV=local
APP_KEY=base64:oTU0Ok1mmE6x0qEosGKhCSxpQLAlBAnNreH7sFAKkdM=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=new_db
DB_USERNAME=root
DB_PASSWORD=

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

PUSHER_KEY=
PUSHER_SECRET=
PUSHER_APP_ID=
  • When the above error appeared on the first time(when I loaded login page), I cleared my browser cache and history, the system managed to back to normal.
  • When the same error appeared on second time(in the mid of testing process), I did the same as first time, cleared browser cache and history, but it could not helped, the error still show after I refreshed my browser. So I executed php artisan view:clear and php artisan cache:clear but still failed to get back to normal.

Anyone have any idea on why is this happening? Thanks

The version I used:

  • OS: Window 7
  • Browser: Mozilla Firefox
  • PHP: 7.1.1
  • Laravel: 5.3

图片转代码服务由CSDN问答提供 功能建议

我用Laravel 5.3测试我的系统:

过去几周 我的系统工作正常。 自上周末以来,我遇到了同样的错误 TWICE ,如下所示: -

注意:未定义的变量:C:\ NewFolder \ htdocs中的_ENV 第303行上的\ project \ vendor \ vlucas \ phpdotenv \ src \ Loader.php

警告:array_key_exists()期望参数2为数组,在C:\ NewFolder \ htdocs \中给出null 第303行的project \ vendor \ vlucas \ phpdotenv \ src \ Loader.php

Loader.php:

  public function getEnvironmentVariable($ name)
 {
 switch(true){
 case array_key_exists($ name,$ _ ENV):// line 303 here 
 return $ _ENV [$ name  ]; 
 case array_key_exists($ name,$ _SERVER):
返回$ _SERVER [$ name]; 
默认值:
 $ value = getenv($ name); 
返回$ value === false?  null:$ value; 
} 
} 
   
 
 

.env

  APP_ENV =本地
APP_KEY = BASE64:oTU0Ok1mmE6x0qEosGKhCSxpQLAlBAnNreH7sFAKkdM = 
APP_DEBUG =真
APP_LOG_LEVEL =调试
APP_URL = HTTP://本地主机
 
DB_CONNECTION = MySQL的
DB_HOST =本地主机
DB_PORT = 3306 
DB_DATABASE = new_db 
DB_USERNAME =根\  nDB_PASSWORD = 
 
BROADCAST_DRIVER = LOG 
CACHE_DRIVER =文件
SESSION_DRIVER =文件
QUEUE_DRIVER =同步
 
REDIS_HOST = 127.0.0.1 
REDIS_PASSWORD = NULL 
REDIS_PORT = 6379 
 
MAIL_DRIVER = SMTP 
MAIL_HOST = mailtrap.io 
MAIL_PORT  = 2525 
MAIL_USERNAME = null 
MAIL_PASSWORD = null 
MAIL_ENCRYPTION = null 
 
PUSHER_KEY = 
PUSHER_SECRET = 
PUSHER_APP_ID = 
   
 
 
  • 当上述情况时 错误出现在第一次(当我加载登录页面时),我清除了我的浏览器缓存和历史记录,系统设法恢复正常。
  • 当第二次出现相同的错误时(在 在测试过程的中间),我第一次做了同样的事情,清除了浏览器缓存和历史记录,但它无济于事 ,我刷新浏览器后仍然显示错误。 所以我执行了 php artisan view:clear php artisan cache:clear 但仍未能恢复正常。 \ n

    任何人都知道为什么会这样? 谢谢

    我使用的版本:

    • 操作系统:Window 7
    • 浏览器:Mozilla Firefox
    • PHP:7.1.1
    • Laravel:5.3
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doushih06137 2018-02-09 02:28
    已采纳

    After some finding and research, I found there are some similar issues from Github #8191.

    I able to get rid of the error by running php artisan config:cache. Means that Laravel will read the environment variable from the config file rather than reading the environment variable directly from .env

    评论
    解决 无用
    打赏 举报
查看更多回答(2条)

相关推荐 更多相似问题