In laravel 4 we had:
$env = $app->detectEnvironment(array(
'local' => array('homestead')
));
by default.
But in laravel 5 it's changed to:
$env = $app->detectEnvironment(function()
{
return getenv('APP_ENV') ?: 'production';
});
Also, they have excluded .env.* line in .gitignore, now it has:
.env
And added file .env.example:
APP_ENV=local
APP_KEY=SomeRandomString
DB_USERNAME=homestead
DB_PASSWORD=homestead
So, if i have more than 2 environments, do i have to set all of them in a single .env file now? E.g.:
APP_ENV=local
DB_PASSWORD=123
APP_ENV=alpha
DB_PASSWORD=456
If i would have no .env file, how laravel will know what environment i am using?
I just wanted to contribute my solution for Laravel 5.1, which is slightly simpler IMHO. In bootstrap/app.php, I have (just after where the Application is instantiated):
There's no need for any checking or error handling. Laravel will default to "production" if the file is not found.
That is all.
For those who just upgraded to 5.2:
You cannot longer use the static
Dotenv::load()
method. Use the following instead:in
bootstrap/app.php
.//edit Soo.. after digging into this for the past hour I might as well add some additional info here:
env()
helper function or directly via PHP's nativegetenv()
function. Although you should only do so to fill your config files (see/config/*.php
), because those can be cached.(new Dotenv($app->environmentPath(), $app->environmentFile()))->load();
: Since it usesload()
any environment value that has already been set will not be overwritten! (You would have to useoverload()
to do so - this drove me nuts because homestead sets theAPP_ENV
variable tolocal
in the php-fpm config/etc/php/7.0/fpm/php-fpm.conf
and you cannot change it via .env file)TestCase
, which sets theAPP_ENV
variable to testing (viarefreshApplication()
-- usingputenv()
to override the defaultlocal
value)You can check on how to setup properly. How to Setup Multiple Environment for Laravel 5 Developers Way
Based on Marcin Nabiałek, I removed some part of it, and put it in proper file to load it right.
You can do it exactly the same as in Laravel 4:
*.env
file are just used to put sensitive data that shouldn't be put into VCS. The same is in Laravel 4but is seems that in last days default detectEnvironment was changed to:
so you can use either setting variable from PC name or from ENV file.
If you use ENV based environment detection in main env file (by default
.env
file you need to add:Of course
local
here is local environment, you can change it intoproduction
ordev
At the moment the most important issue I see is that you need to remember when going on production to change this
.env
file content fromAPP_ENV=local
toAPP_ENV=production
so in my opinion much better method is the old default method based on PC names.Now ENV files. If you use ENV based environment detection, you should put into your ENV file only:
Now you can create separate ENV files for your different environments for example:
.local.env :
.production.env :
and now in
bootstrap.environment.php
file you can modfiy:into:
to load extra env file based on
APP_ENV
from main env file.Now you will be able to use it in your other configuration file as always:
$_ENV['MY_DB']
The fact that you can't have more than one
.env
file by default and that it's excluded in .gitignore is intentional and is the intended way to manage environments. The.env
file should not be in version control and should be configured per environment..env
sets your environment and all environment variables.No. You would have a
.env
file in each place that you have your application installed. The difference is in what is inside that file.Additionally, because the
.env
file is simply a key-value store, any subsequent declarations would overwrite previous ones. In your example, Laravel would never see your "local" settings.It seems odd at first, but this new default system is actually generally easier and less prone to the issues the "4.2 way" had/has, as there's no place for logic errors.
It wouldn't run at all. In the
.env
file is also anAPP_KEY
declaration, which Laravel will not run without. Without a.env
file, you would get a 500 server error.