使用Symfony 4命令加载不同的.env文件 [英] Load different .env file with a Symfony 4 command
问题描述
.env
文件在运行Symfony 4命令时被解析(如果可以使用dotenv)。
.env
file is parsed when running a Symfony 4 command (if dotenv is available).
此在开发时工作正常,而且我想测试我的代码(因此是另一种环境),因此我需要加载另一个 .env
文件。
This is working fine when developping, but also, I want to test my code (so another environment), hence I need to load another .env
file.
我喜欢Docker运行容器的操作:
I love what Docker did to run a container:
docker run -e MYVAR1 --env MYVAR2=foo --env-file ./env.list ubuntu bash
所以我正在寻找使用Symfony实现相同的功能:
So I am looking to achieve same thing with Symfony:
php bin/console --env-file ./.env.test
现在,我正在这样做:
export $(grep -v '^#' .env.test | xargs) && php bin/console
推荐答案
我选择编辑 bin / console
文件直接创建另一个 .env
文件,这不是问题,因为它是开发人员拥有的文件掌控。我将相关部分更新为;
I opted for editing the bin/console
file directly to facilitate the different .env
file, which isn't an issue since it's a file the developer has control over. I updated the relevant section to;
if (!isset($_SERVER['APP_ENV'])) {
if (!class_exists(Dotenv::class)) {
throw new \RuntimeException('APP_ENV environment variable is not defined. You need to define environment variables for configuration or add "symfony/dotenv" as a Composer dependency to load variables from a .env file.');
}
}
$input = new ArgvInput();
$env = $input->getParameterOption(['--env', '-e'], $_SERVER['APP_ENV'] ?? 'dev', true);
switch ($env) {
case 'test':
(new Dotenv())->load(__DIR__.'/../.env.test');
break;
case 'dev':
default:
(new Dotenv())->load(__DIR__.'/../.env');
break;
}
这篇关于使用Symfony 4命令加载不同的.env文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!