Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel environment files for local and production possibly not working as intended?

In Laravel 4, I'm setting up my local development machine (my laptop) and then I have my production server.

In my /bootstrap/start.php

$env = $app->detectEnvironment(array(

'local'      => array('MacBook-Pro-2.local'),
'production' => array('ripken'),

));

I then created a .env.local.php in my root directory with the following:

<?php

return [

// Database Connection Settings
'db_host'     => '127.0.0.1',
'db_name'     => 'censored',
'db_user'     => 'censored',
'db_password' => 'censored'

];

That part works. However, I went to my production server and created the same exact file, called it .env.production.php and it doesn't load the variables from that file. I don't know what made me think to do it but I renamed that file .env.php without "production" in the name and it works.

My question therefore is, why wouldn't .env.production.php work? I thought that is what the good folks over at Laravel wanted me to name my various environment files.

like image 796
Chris Farrugia Avatar asked Jan 10 '23 12:01

Chris Farrugia


1 Answers

Production is the 'default' ruleset, so it is a bit special. Laravel will look for the "base" or "default" rules when loading production, rather than the actual name production.

So you can change

$env = $app->detectEnvironment(array(
'local'      => array('MacBook-Pro-2.local'),
'production' => array('ripken'),
));

to

$env = $app->detectEnvironment(array(
'local'      => array('MacBook-Pro-2.local'),    
));

That way anything that is not "MacBook-Pro-2.local" is going to be production automatically. Then just use the default .env.php for the production settings.

Every other env needs to be explicitly defined - such as .env.local.php and .env.testing.php etc

like image 52
Laurence Avatar answered Jan 17 '23 18:01

Laurence