Laravel5.1项目升级到Laravel5.5后,对env文件做了一些校验,例如配置项中不能有空格就是其中一个点,今天在开发过程中刚好遇到,写个帖子记录一下。

报错内容:

[2019-03-13 08:46:02] production.ERROR: Dotenv values containing spaces must be surrounded by quotes. {"exception":"[object] (Dotenv\\Exception\\InvalidFileException(code: 0): Dotenv values containing spaces must be surrounded by quotes. at /home/work/wwwserver/dev_ichenhua_cn/vendor/vlucas/phpdotenv/src/Loader.php:228)"} []

问题背景:

location ~ \.php$ {
        fastcgi_param  APP_ENV test;

.env 文件中 APP_ENV=test

以上两种方式都可以设置环境变量。

问题根源:

1、Laravel5在加载配置文件时,会根据当前环境变量,加载相应配置文件。

2、根目录存在 .env.test 文件,而且在该文件中,一个数组的配置存在空格,所以报错。

问题解决:

1、删除 .env.test 文件,让系统加载 .env 文件

2、去掉 .env 文件中,配置项的空格


源码分析:

\vendor\laravel\framework\src\Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables.php
$this->setEnvironmentFilePath(
    $app, $app->environmentFile().'.'.env('APP_ENV')
);
protected function setEnvironmentFilePath($app, $file)
{
    if (file_exists($app->environmentPath().'/'.$file)) {
        $app->loadEnvironmentFrom($file);
        return true;
    }
    return false;
}

本文为 陈华 原创,欢迎转载,但请注明出处:http://www.ichenhua.cn/read/13