I try to follow the Symfony 3.4 documentation in order to use the environment variables and their default value https://symfony.com/doc/3.4/configuration/external_parameters.html
As the documentation suggest, i try to put the env parameter and his default value:
parameters:
env(DATABASE_HOST): 127.0.0.1
database_host: '%env(DATABASE_HOST)%'
But when i try to execute composer, composer dump-autoload --optimize && composer run-script post-install-cmd
I have this exception
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache
In EnvVarProcessor.php line 76:
Environment variable not found: "DATABASE_HOST".
Script Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache handling the post-install-cmd event terminated with an exception
[RuntimeException]
An error occurred when executing the "'cache:clear --no-warmup'" command:
In EnvVarProcessor.php line 76:
Environment variable not found: "DATABASE_HOST".
run-script [--timeout TIMEOUT] [--dev] [--no-dev] [-l|--list] [--] [<script>] [<args>]...
Look like that composer ignore the default parameter value.
I got almost the same scenario; I tried to maker migration from Symfony 3.3 => 3.4 and I got excited to use Environment Variable Processors but need to be configure:
After reading how to use https://symfony.com/doc/current/components/dotenv.html is not enough here is the steps how to configure Dotenv
run command bellow to update composer.json composer require --dev symfony/dotenv
web/app_dev.yml
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Debug\Debug;
use Symfony\Component\Dotenv\Dotenv;
...
/**
* @var Composer\Autoload\ClassLoader $loader
*/
$loader = require __DIR__.'/../vendor/autoload.php';
Debug::enable();
$dotenv = new Dotenv();
$dotenv->load(__DIR__.'/../.env');
$kernel = new AppKernel('dev', true);
...
bin/console.php
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Debug\Debug;
use Symfony\Component\Dotenv\Dotenv;
...
$loader = require __DIR__.'/../vendor/autoload.php';
$input = new ArgvInput();
$env = $input->getParameterOption(array('--env', '-e'), getenv('SYMFONY_ENV') ?: 'dev');
$debug = getenv('SYMFONY_DEBUG') !== '0' && !$input->hasParameterOption(array('--no-debug', '')) && $env !== 'prod';
if ($env === 'dev') {
$dotenv = new Dotenv();
$dotenv->load(__DIR__.'/../.env');
}
if ($debug) {
Debug::enable();
}
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.