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
Laravel 5.2 not reading env file
提问by andrewtweber
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
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 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 的新副本。我基本上只复制到我的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:cache
command during deployment, you mustmake sure that you are only calling theenv
function from within your configuration files, and not from anywhere else in your application.If you are calling
env
from within your application, it is strongly recommended you add proper configuration values to your configuration files and callenv
from that location instead, allowing you to convert yourenv
calls toconfig
calls.
如果
config:cache
在部署期间使用该命令,则 必须确保仅从env
配置文件中调用该函数,而不是从应用程序中的任何其他位置调用该函数。如果您
env
从应用程序内部调用,强烈建议您将适当的配置值添加到配置文件并env
从该位置调用,这样您就可以将env
调用转换为config
调用。
Reference: https://laravel.com/docs/5.2/upgrade#upgrade-5.2.0
回答by benjolly1989
If any of your .env
variables 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.php
and I solved using config clear
and optimize
commands:
我有一个类似的问题,我config/services.php
解决了使用config clear
和optimize
命令:
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
thenphp artisan config:cache
然后php artisan config:cache
回答by jay_mziray
I had the same issue on local environment, I resolved by
我在本地环境上遇到了同样的问题,我解决了
- php artisan config:clear
- php artisan config:cache
- and then cancelling php artisan serve command, and restart again.
- php工匠配置:清除
- php工匠配置:缓存
- 然后取消 php artisan serve 命令,重新启动。
回答by dKen
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')
将 env 配置选项添加到您的
app.php
配置文件中,如下所示:'env' => env('APP_ENV', 'production')
Adding this line got the local .env
file to be read in correctly.
添加这一行.env
可以正确读取本地文件。
回答by Shahrukh Anwar
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 config:cache
,它将清除所有env
变量并env()
给出空值,尝试运行以下命令并在那里env()
再次开始捕获所有env
变量
php artisan config:clear