Reputation: 195
I have the following setup with Laravel 4.2:
bootstrap/start.php (hostname correct, environment is local)
$env = $app->detectEnvironment(array(
'production' => array('produrl'),
'local' => array('MBP-Ivo.local', 'iMac-Ivo.local'),
));
.env.local.php (in project root, .env.php is exactly the same except mysql info)
<?php
return [
// Code variables
'mysqlUsername' => 'user',
'mysqlPassword' => 'password',
'mysqlDatabase' => 'database',
'paymentIdeal' => false,
'shipmentCountries' => [
'Nederland' => 'Nederland',
'Belgie' => 'Belgie'
]
];
config/app.php (I don't overwrite with app/config/local/app.php)
<?php
return array(
'paymentIdeal' => $_ENV['paymentIdeal'],
'shipmentCountries' => $_ENV['shipmentCountries']
);
There are some more variables, but the problem is with shipmentCountries.
Undefined index: shipmentCountries
All variables declared are working (eg paymentIdeal), but shipmentCountries gives an error. I think because it's an array? The name is exactly the same everywhere, including capital letters.
Does anyone know why I can't get this working?
BTW: I'm choosing this option to prevent users having to change their application configs. I want to use only one *.env file to configure all important stuff. And yes, I know these values could be saved to the database, but that's for later :)
/edit: When I dump the $_ENV, I get the following:
Array
(
[mysqlUsername] => ***
[mysqlPassword] => ***
[mysqlDatabase] => ***
[paymentIdeal] =>
[shipmentCountries.Nederland] => Nederland
[shipmentCountries.Belgie] => Belgie
);
Somehow it "flattens" the array shipmentCountries. Anyone knows how and why?
Upvotes: 2
Views: 6482
Reputation: 404
Another way is to json_encode your associative array in the env.local.php, then in your config
json_decode($_ENV['shipmentCountries'],true);
Don't forget the boolean argument there to make it convert into arrays.
Upvotes: 0
Reputation: 153140
You're right, the file get's converted in a flat array using the dot notation (with array_dot
)
I believe the reason behind this, is that environment variables are just not supposed to be arrays as they are normally passed in when using a CLI.
So, what can you do about it?
In your app/start/global.php
use this code to convert the array back to it's original format:
$array = array();
foreach ($_ENV as $key => $value) {
array_set($array, $key, $value);
}
$_ENV = $array;
Also inside app/start/global.php
(this would be .my.env.local.php
)
$config = require base_path().'/.my.env.'.app()->env.'.php';
$_ENV = array_merge($_ENV, $config);
I'd think again if you really don't want to use config files. It is possible to have your own config file and maybe you can even place it in the root of the project.
Also I'd change the array to a numeric one:
'shipmentCountries' => [
'Nederland',
'Belgie'
]
Upvotes: 1
Reputation: 195
With the tip of lukasgeiter, I went searching again, and found this:
https://github.com/laravel/framework/issues/5291 and https://github.com/laravel/framework/pull/4623
It looks like Laravel doesn't support this option.
What I do now is save it as a JSON string, and decode it when neccesary.
Upvotes: 0