Ivo Geersen Ivo Geersen - 13 days ago 6
PHP Question

Laravel environment variables: Undefined Index for array

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?

Answer

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.