Access all settings of the .env file

1

Does anyone know how I can access all the data that is in the .env file? I need to bring all the data as an array, I have already used the helper function env (), without success, I need something like the example below. p>

$dadosConn = [
'driver' => 'postgres',
'username' => '',
'password' => '',
'host' => '',
'database' => '',
'port' => ''];
    
asked by anonymous 26.02.2016 / 15:44

2 answers

1

Well, you could actually solve the problem by using the built-in resource used by Laravel 5 . This is a library (already installed in Laravel 5 ) called Dotenv .

See how to use:

$dot = new Dotenv\Dotenv(base_path());
dd($dot->load());

The result is:

array:20 [▼
  0 => "APP_ENV=local"
  1 => "APP_DEBUG=true"
  2 => "APP_KEY=PzWPA7lYetAsN9aGmHuSsdVaNh7DfCjt"
  3 => "APP_URL=http://localhost"
  4 => "DB_HOST=127.0.0.1"
  5 => "DB_DATABASE=homestead"
  6 => "DB_USERNAME=homestead"
  7 => "DB_PASSWORD=secret"
  8 => "CACHE_DRIVER=file"
  9 => "SESSION_DRIVER=file"
  10 => "QUEUE_DRIVER=sync"
  11 => "REDIS_HOST=127.0.0.1"
  12 => "REDIS_PASSWORD=null"
  13 => "REDIS_PORT=6379"
  14 => "MAIL_DRIVER=smtp"
  15 => "MAIL_HOST=mailtrap.io"
  16 => "MAIL_PORT=2525"
  17 => "MAIL_USERNAME=null"
  18 => "MAIL_PASSWORD=null"
  19 => "MAIL_ENCRYPTION=null"
]

The information is returned in this way, as it was not meant to be used as array , but to be used by the getenv function. The Dotenv internally uses the putenv function to save the information of the .env file.

    
26.02.2016 / 16:48
0

You can try this:

var_dump(getenv('DB_HOST'));
var_dump(getenv('DB_DATABASE'));
var_dump(getenv('DB_USERNAME'));
var_dump(getenv('DB_PASSWORD'));

If you want to put it in an array it will look like this:

$dadosBanco = [
    'host'     => getenv('DB_HOST'),
    'database' => getenv('DB_DATABASE'),
    'username' => getenv('DB_USERNAME'),
    'password' => getenv('DB_PASSWORD')
];

I do not work very much with Laravel, but I think you could configure it in the config/database.php file, which would be the most appropriate. There you can create multiple connection settings. It will depend on the need.

    
26.02.2016 / 16:08