6

我正在尝试在 Amazon Elastic Beanstalk 中上传 Lumen 项目。

.env 在 .gitignore 中。

没关系,因为我有几个环境(dev、qa、prod),所以我需要为每个环境配置单独的环境变量

我收到此错误消息:

 Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Dotenv: Environment file .env not found or not readable. Create file with your environment settings at /var/app/current/bootstrap/../.env' in /var/app/current/vendor/vlucas/phpdotenv/src/Dotenv.php:33 Stack trace: #0 /var/app/current/bootstrap/app.php(4): Dotenv::load('/var/app/curren...') #1 /var/app/current/public/index.php(13): require('/var/app/curren...') #2 {main} thrown in /var/app/current/vendor/vlucas/phpdotenv/src/Dotenv.php on line 33

我了解系统找不到 .env

事情是我在 Amazon Console 中设置了变量:

Software Configuration
Environment variables: APP_ENV, DB_USERNAME, DB_PASSWORD, DB_DATABASE,  DB_HOST, APP_KEY

eb 打印环境:

 Environment Variables:
  DB_DATABASE = ebdb
  DB_PASSWORD = xxxxxxxx
  APP_KEY = xAY4hnrXlht5fdvB9PzPAwDqc1R
  DB_HOST = xxxxxxcnzd3rux8ue7.us-east-1.rds.amazonaws.com:3306
  APP_ENV = dev
  DB_USERNAME = myuser

我也有 .ebextensions/environment.config :

 container_commands:
 # Copy EB env configuration file over
 01_config_environment:
  command: mv /var/app/ondeck/.env.elasticbeanstalk /var/app/ondeck/.env
 02-install-packages:
command: "composer.phar install -d /var/app/ondeck/www"
 option_settings:
 option_name: DB_HOST
 value: xxxxxxx.cnzd3rux8ue7.us-east-1.rds.amazonaws.com
- option_name: DB_PORT
 value: 3306
- option_name: DB_NAME
 value: ebdb
- option_name: DB_USER
 value: myuser
- option_name: DB_PASS
 value: xxxxxx

但是无法摆脱这个错误!

4

1 回答 1

0

流明 5.0、5.1

如果您使用的是 Lumen < 5.2,则可以更新bootstrap/app.php正在加载 .env 文件的文件。只需捕获异常并忽略它。

try {
    Dotenv::load(__DIR__.'/../');
} catch (InvalidArgumentException $e) {
    //
}

Lumen 5.2 - 5.7 没有这个问题,因为他们基本上已经完成了上述工作。

流明 5.8+

Lumen 5.8 重新引入了这个问题,也让它更难解决。为此,您需要创建一个扩展类\Laravel\Lumen\Bootstrap\LoadEnvironmentVariables并覆盖该bootstrap()方法。例如,创建一个新文件app/Bootstrap/MyLoadEnvironmentVariables.php

<?php

namespace App\Bootstrap;

use Dotenv\Exception\InvalidFileException;
use Laravel\Lumen\Bootstrap\LoadEnvironmentVariables;

class MyLoadEnvironmentVariables extends LoadEnvironmentVariables
{
    public function bootstrap()
    {
        try {
            $this->createDotenv()->safeLoad();
        } catch (InvalidFileException $e) {
            //
        }
    }
}

现在,您需要更新bootstrap/app.php文件以使用新类而不是基类:

(new App\Bootstrap\MyLoadEnvironmentVariables(
    dirname(__DIR__)
))->bootstrap();
于 2020-12-24T21:37:44.640 回答