读取环境变量在Laravel 5.1配置文件中的Vhosts中设置

Working with laravel 5.1. So in my apache vhost I've set the environment variable 'ENV_DEV' to one. If I print_r($_SERVER) from vanilla PHP or even in a controller I can see the variable is set.

I wanted to insert code into config/database.php to detect environments and alter the database connections, however for some reason I get the following error running artisan:

if ($_SERVER['ENV_DEV'] == 1) { ...dbconfig here... }

php artisan migrate

PHP Notice:  Undefined index: ENV_DEV in /www/config/database.php on line 2

I understand declaring it in .env is a solution, but my goal is to use .env for common settings across a range of environments and handle the DB config further upstream.

Is there a work around?

If you want to specify env name for console, just run script with param --env=your_env:

php artisan migrate --env=your_env

If you want to specify env for web requests, you may define web server parameter. Example for Apache:

SetEnv LARAVEL_ENV your_env

And modify bootstrap/environment.php:

...
if (empty($curEnv))
{
    // Use LARAVEL_ENV if defined
    $curEnv = getenv('LARAVEL_ENV') ?: 'local';
}
...

Like others wrote, environment variables from apache don't automatically trickle down into command-line commands. It is however possible to provide them to the commands from the shell. e.g.: ENV_DEV=1 php artisan migrate