php Laravel 5.2 不读取 env 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34420761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:58:34  来源:igfitidea点击:

Laravel 5.2 not reading env file

phplaravelenvironment-variableslaravel-5.2

提问by andrewtweber

After upgrading to Laravel 5.2, none of my .envfile values are being read. I followed the upgrade instructions; none of my config files were changed except auth.php. They were all working fine in previous version, 5.1.19

升级到 Laravel 5.2 后,我的.env文件值都没有被读取。我按照升级说明进行操作;除了 auth.php,我的配置文件都没有改变。它们在以前的版本 5.1.19 中都运行良好

.envcontains values such as

.env包含值,例如

DB_DATABASE=mydb
DB_USERNAME=myuser

config/database.phpcontains

config/database.php包含

'mysql' => [
    'database' => env('DB_DATABASE', 'forge'),
    'username' => env('DB_USERNAME', 'forge'),
]

I get this error:

我收到此错误:

PDOException: SQLSTATE[HY000] [1045] Access denied for user 'forge'@'localhost' (using password: NO)

Clearly not pulling in my env config. This is affecting every single one of my config files, including third party such as bugsnag.

显然没有拉入我的 env 配置。这影响了我的每一个配置文件,包括第三方,如 bugsnag。

I also tried

我也试过

php artisan config:clear
php artisan cache:clear

Update

更新

Trying php artisan tinker

php artisan tinker

>>> env('DB_DATABASE')
=> null
>>> getenv('DB_DATABASE')
=> false
>>> config('database.connections.mysql.database')
=> "forge"
>>> dd($_ENV)
[]

I have tried installing a fresh copy of Laravel 5.2. I basically only copied in my appfolder; no additional composer packages are included. Still having the same issue. I have other Laravel 5.2 projects on the same server that are working fine.

我尝试安装 Laravel 5.2 的新副本。我基本上只复制到我的app文件夹中;不包括额外的作曲家包。仍然有同样的问题。我在同一台服务器上还有其他运行良好的 Laravel 5.2 项目。

采纳答案by andrewtweber

Wow. Good grief. It's because I had an env value with a space in it, not surrounded by quotes

哇。好伤心。这是因为我有一个 env 值,里面有一个空格,没有被引号包围

This

这个

SITE_NAME=My website

Changed to this

改成这个

SITE_NAME="My website"

Fixed it. I think this had to do with Laravel 5.2 now upgrading vlucas/phpdotenvfrom 1.1.1 to 2.1.0

修复。我认为这与 Laravel 5.2 现在将vlucas/phpdotenv从 1.1.1升级到 2.1.0 有关

回答by Gaurav Gupta

From the official Laravel 5.2 Upgrade Notes:

来自官方 Laravel 5.2 升级说明:

If you are using the config:cachecommand during deployment, you mustmake sure that you are only calling the envfunction from within your configuration files, and not from anywhere else in your application.

If you are calling envfrom within your application, it is strongly recommended you add proper configuration values to your configuration files and call envfrom that location instead, allowing you to convert your envcalls to configcalls.

如果config:cache在部署期间使用该命令,则 必须确保仅从env配置文件中调用该函数,而不是从应用程序中的任何其他位置调用该函数。

如果您env从应用程序内部调用,强烈建议您将适当的配置值添加到配置文件并env从该位置调用,这样您就可以将env调用转换为config调用。

Reference: https://laravel.com/docs/5.2/upgrade#upgrade-5.2.0

参考:https: //laravel.com/docs/5.2/upgrade#upgrade-5.2.0

回答by benjolly1989

If any of your .envvariables contains white space, make sure you wrap them in double-quotes. For example:

如果您的任何.env变量包含空格,请确保将它们用双引号括起来。例如:

SITE_NAME="My website"

SITE_NAME="My website"

Don't forget to clear your cache before testing:

在测试之前不要忘记清除缓存:

php artisan config:cache
php artisan config:clear

回答by Deric Lima

I had a similar issue in my config/services.phpand I solved using config clearand optimizecommands:

我有一个类似的问题,我config/services.php解决了使用config clearoptimize命令:

php artisan config:clear
php artisan optimize

回答by beatusfk

For me the following worked

对我来说,以下工作

- php artisan config:cache
- php artisan config:clear
- php artisan cache:clear

回答by TrOnNe

For me it has worked this in this order:

对我来说,它按以下顺序工作:

php artisan config:cache
php artisan config:clear
php artisan cache:clear

And I've tried all the rests without luck.

而且我已经尝试了所有其余的方法而没有运气。

回答by Ganesh K

run this:

运行这个:

php artisan config:clear
php artisan cache:clear

then
php artisan config:cache

然后
php artisan config:cache

回答by jay_mziray

I had the same issue on local environment, I resolved by

我在本地环境上遇到了同样的问题,我解决了

  1. php artisan config:clear
  2. php artisan config:cache
  3. and then cancelling php artisan serve command, and restart again.
  1. php工匠配置:清除
  2. php工匠配置:缓存
  3. 然后取消 php artisan serve 命令,重新启动。

回答by dKen

I missed this in the upgrade instructions:

我在升级说明中错过了这一点:

Add an env configuration option to your app.phpconfiguration file that looks like the following: 'env' => env('APP_ENV', 'production')

将 env 配置选项添加到您的app.php配置文件中,如下所示: 'env' => env('APP_ENV', 'production')

Adding this line got the local .envfile to be read in correctly.

添加这一行.env可以正确读取本地文件。

回答by Shahrukh Anwar

When you fired command php artisan config:cachethen it will wipe out all the envvariables and env()will give null values, try running follwing command and boom there your env()again begin to catch all envvariable

当您触发命令时php artisan config:cache,它将清除所有env变量并env()给出空值,尝试运行以下命令并在那里env()再次开始捕获所有env变量

php artisan config:clear