Access all file settings. env

Asked

Viewed 1,454 times

0

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

$dadosConn = [
'driver' => 'postgres',
'username' => '',
'password' => '',
'host' => '',
'database' => '',
'port' => ''];
  • Sorry to ask, but no . env does not have much data that would be useful to me, what the reason exactly?

  • I need to precisely access the connection data and then use it to generate reports using the Jasperphp library. So it would be nice if I could access the connection data directly from . env.

  • What getenv('APP_ENV') returns?

  • returns: "local"

3 answers

2

Well, actually, you could solve the problem using the internal resource used by Laravel 5. It is a library (which is already installed in the 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 this way, as it was not meant to be used as array, and yes to be used by the function getenv. The Dotenv internally uses the function putenv, to save file information .env.

  • Thanks for the explanation.

0

You can try it:

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 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 don’t work much with Laravel, but I think you could configure in the file config/database.php, what would be the most indicated. There you can create multiple connection settings. It will depend on the need.

  • Actually I would like to use somehow the helper env(), currently I can capture all my connection settings, however I would like something more "elegant".

  • @"elegant" geekcom can have many interpretations, you can create a class or anything of the kind, but the important thing is to be able to do the basic, if you want to "decorate later" is already a choice of yours, try to let go of it and understand the lowest level, then you create objects, arrays, classes, etc. See an "Array" example that adds ;)

-4

In the Laravel framework if you want to use the value of a file variable. env and is returning null, need to clear the settings cache with the following command below:

php artisan config:clear

Once you execute this command do the test that if the variable has a value it will not return null.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.