We have three development servers (dev1, dev2, stage) and one production server and some symfony projects.
We would like to change the environment automatically. Currently my hack looks like this in 'public/index.php'
use App\Kernel;
use Symfony\Component\Debug\Debug;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\HttpFoundation\Request;
require __DIR__.'/../vendor/autoload.php';
//----------- hack start ------------
$_SERVER['APP_ENV'] = 'prod';
$_SERVER['APP_DEBUG'] = 0;
$_SERVER['APP_SECRET'] = '67d829bf61dc5f87a73fd814e2c9f629';
$localIP = getHostByName(getHostName());
if ($localIP === '81.4.552.97') {
$_SERVER['APP_ENV'] = 'dev1';
$_SERVER['APP_DEBUG'] = 1;
$_SERVER['APP_SECRET'] = '67d829bf61dc5f87a73fd814e2c9f629';
}
...
//----------- hack end ------------
// The check is to ensure we don't use .env in production
if (!isset($_SERVER['APP_ENV'])) {
(new Dotenv())->load(__DIR__.'/../.env');
}
if ($_SERVER['APP_DEBUG'] ?? ('prod' !== ($_SERVER['APP_ENV'] ?? 'dev'))) {
umask(0000);
Debug::enable();
}
// Request::setTrustedProxies(['0.0.0.0/0'], Request::HEADER_FORWARDED);
$kernel = new Kernel($_SERVER['APP_ENV'] ?? 'dev', $_SERVER['APP_DEBUG'] ?? ('prod' !== ($_SERVER['APP_ENV'] ?? 'dev')));
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
and I have three new environments under 'config/packages/*' including the configuration for database and email.
Is this the right way? Maybe some others also have this problem. I just tested it under dev1, it seems to be running.