2
votes

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?

3
What does a var_dump of $_ENV show?lukasgeiter
See above, it somehow "flattens" the array? But why...?Ivo Geersen

3 Answers

1
votes

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?

Convert the array from dot to non-dot

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;

Use another file and load it yourself

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);

Sidenotes

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'
]
0
votes

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.

0
votes

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.