首页 > 解决方案 > Laravel 项目在没有 .env 文件的情况下提供服务

问题描述

如果 .env 在我的 Laravel 项目中被删除,则命令

$ php artisan key:generate

导致此错误

ErrorException  : file_get_contents(/folder/projectlocation/.env): failed to open stream: No such file or directory at //folder/projectlocation/vendor/laravel/framework/src/Illuminate/Foundation/Console/KeyGenerateCommand.php:96


{

file_put_contents(
$this->laravel->environmentFilePath(), 

preg_replace(

$this->keyReplacementPattern(),

'APP_KEY='.$key,

file_get_contents($this->laravel->environmentFilePath())

));

}

但是该php artisan serve命令有效"Laravel development server started: <http://127.0.0.1:8000>"

我已经清除了所有 Laravel 缓存。为什么它会服务但不获取 .env 文件配置?

标签: laravel

解决方案


那是对的。所有应用程序配置都来自文件config/夹中的配置文件。此处的每个选项都配置为采用.env文件中的值或默认值。

例如,config/app.php文件中的应用程序密钥:

'key' => env('APP_KEY'),

APP_KEY因此,当没有为in设置值时.env,应用程序密钥将是null(不推荐!)。


推荐阅读