Laravel 5.2不读取env文件

时间:2022-10-18 15:04:35

After upgrading to Laravel 5.2, none of my .env file 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中,他们都运行得很好

.env contains values such as

.env包含如下值

DB_DATABASE=mydb
DB_USERNAME=myuser

config/database.php contains

配置/数据库。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配置拉进来。这影响到我的每一个配置文件,包括第三方,如bug谋求。

I also tried

我也试过

php artisan config:clear
php artisan cache:clear

Update

更新

Trying php artisan tinker

在php工匠修补

>>> 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 "app" folder; 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的新版本。我基本上只复制在我的“应用”文件夹里;不包括额外的编写程序包。仍然有同样的问题。我在同一台服务器上有其他Laravel 5.2项目正在正常工作。

13 个解决方案

#1


44  

From the official Laravel 5.2 Upgrade Notes:

官方的Laravel 5.2升级说明:

If you are using the config:cache command during deployment, you must make sure that you are only calling the env function from within your configuration files, and not from anywhere else in your application.

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

If you are calling env from within your application, it is strongly recommended you add proper configuration values to your configuration files and call env from that location instead, allowing you to convert your env calls to config calls.

如果您在应用程序中调用env,强烈建议您向配置文件中添加适当的配置值,并从该位置调用env,从而允许您将env调用转换为配置调用。

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

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

#2


29  

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/phpdotenv from 1.1.1 to 2.1.0

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

#3


18  

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

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

SITE_NAME="My website"

:域名=“我的网站”

Don't forget to clear your cache before testing:

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

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

#4


14  

I had a similar issue in my config/services.php and I solved using config clear and optimize commands:

我的配置/服务中也有类似的问题。php和我使用配置清除和优化命令解决:

php artisan config:clear
php artisan optimize

#5


5  

I missed this in the upgrade instructions:

我在升级说明中漏掉了这一点:

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

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

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

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

#6


5  

The simplicity is the power:

简单就是力量:

php artisan config:cache

You will receive:

你将获得:

Configuration cache cleared!

配置缓存清除!

Configuration cached successfully!

成功配置缓存!

#7


5  

run this:

运行这个:

php artisan config:clear
php artisan cache:clear

or
php artisan config:cache

或php工匠配置:缓存

#8


3  

delete cache using:

删除缓存使用:

    php artisan config:clear
    php artisan config:cache

#9


2  

Also additional to what @andrewtweber suggested make sure that you don't have spaces between the KEY= and the value unless it is between quotes

另外,@andrewtweber还建议确保在KEY=和值之间没有空格,除非是在引号之间

.env file e.g.:

.env文件如:

...
SITE_NAME= My website
MAIL_PORT= 587
MAIL_FROM_NAME= websitename
...

to:

:

...
SITE_NAME="My website"
MAIL_PORT=587
MAIL_FROM_NAME=websitename
...

#10


2  

I ran into this same problem on my local, and I have tried all the answers here but to no avail. Only this worked for me, php artisan config:clear and restart server. Works like a charm!

我在我的家乡遇到了同样的问题,我在这里尝试了所有的答案,但是没有任何用处。只有php artisan config:clear和restart server对我有效。就像一个魅力!

#11


2  

I solved this problem generating a new key using the command: php artisan key:generate

我解决了这个问题,使用这个命令生成一个新的密钥:php artisan键:生成。

#12


2  

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

当您启动命令php artisan配置:缓存时,它将清除所有env变量,env()将给出空值,尝试运行下面的命令,然后在那里启动您的env(),再次开始捕获所有env变量。

php artisan config:clear

#13


0  

If you run this php artisan config:cache command on console then it will store all the .env file contents in cache, after this command if you append any contents into .env file the it will not be not be available until you run php artisan config:clear command

如果您在控制台运行这个php artisan config:cache命令,那么它将在缓存中存储所有.env文件内容,如果您将任何内容添加到.env文件中,那么在运行php artisan config:clear命令之前,它将不可用

#1


44  

From the official Laravel 5.2 Upgrade Notes:

官方的Laravel 5.2升级说明:

If you are using the config:cache command during deployment, you must make sure that you are only calling the env function from within your configuration files, and not from anywhere else in your application.

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

If you are calling env from within your application, it is strongly recommended you add proper configuration values to your configuration files and call env from that location instead, allowing you to convert your env calls to config calls.

如果您在应用程序中调用env,强烈建议您向配置文件中添加适当的配置值,并从该位置调用env,从而允许您将env调用转换为配置调用。

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

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

#2


29  

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/phpdotenv from 1.1.1 to 2.1.0

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

#3


18  

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

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

SITE_NAME="My website"

:域名=“我的网站”

Don't forget to clear your cache before testing:

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

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

#4


14  

I had a similar issue in my config/services.php and I solved using config clear and optimize commands:

我的配置/服务中也有类似的问题。php和我使用配置清除和优化命令解决:

php artisan config:clear
php artisan optimize

#5


5  

I missed this in the upgrade instructions:

我在升级说明中漏掉了这一点:

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

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

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

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

#6


5  

The simplicity is the power:

简单就是力量:

php artisan config:cache

You will receive:

你将获得:

Configuration cache cleared!

配置缓存清除!

Configuration cached successfully!

成功配置缓存!

#7


5  

run this:

运行这个:

php artisan config:clear
php artisan cache:clear

or
php artisan config:cache

或php工匠配置:缓存

#8


3  

delete cache using:

删除缓存使用:

    php artisan config:clear
    php artisan config:cache

#9


2  

Also additional to what @andrewtweber suggested make sure that you don't have spaces between the KEY= and the value unless it is between quotes

另外,@andrewtweber还建议确保在KEY=和值之间没有空格,除非是在引号之间

.env file e.g.:

.env文件如:

...
SITE_NAME= My website
MAIL_PORT= 587
MAIL_FROM_NAME= websitename
...

to:

:

...
SITE_NAME="My website"
MAIL_PORT=587
MAIL_FROM_NAME=websitename
...

#10


2  

I ran into this same problem on my local, and I have tried all the answers here but to no avail. Only this worked for me, php artisan config:clear and restart server. Works like a charm!

我在我的家乡遇到了同样的问题,我在这里尝试了所有的答案,但是没有任何用处。只有php artisan config:clear和restart server对我有效。就像一个魅力!

#11


2  

I solved this problem generating a new key using the command: php artisan key:generate

我解决了这个问题,使用这个命令生成一个新的密钥:php artisan键:生成。

#12


2  

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

当您启动命令php artisan配置:缓存时,它将清除所有env变量,env()将给出空值,尝试运行下面的命令,然后在那里启动您的env(),再次开始捕获所有env变量。

php artisan config:clear

#13


0  

If you run this php artisan config:cache command on console then it will store all the .env file contents in cache, after this command if you append any contents into .env file the it will not be not be available until you run php artisan config:clear command

如果您在控制台运行这个php artisan config:cache命令,那么它将在缓存中存储所有.env文件内容,如果您将任何内容添加到.env文件中,那么在运行php artisan config:clear命令之前,它将不可用