wujt
wujt

Reputation: 1328

Lumen does not read env from system during request

Lumen 5.4, MySql & Docker. I have following variables in global env

$ printenv
DB_HOST=127.0.0.1
DB_DATABASE=database
etc

.env in my project the are present also, but they have different values.

If I type in tinker env('DB_HOST'), it prints value from the global environment, but when application runs, it takes from the specified .env file. I think the problem exists within following function in Laravel\Lumen\Application :

/**
 * Load a configuration file into the application.
 *
 * @param  string  $name
 * @return void
 */
public function configure($name)
{
    if (isset($this->loadedConfigurations[$name])) {
        return;
    }

    $this->loadedConfigurations[$name] = true;

    $path = $this->getConfigurationPath($name);

    if ($path) {
        $this->make('config')->set($name, require $path);
    }
}

How to override those values or make it to avoid those conditions: isset($this->loadedConfigurations[$name]) ?

Upvotes: 2

Views: 2652

Answers (1)

Ohgodwhy
Ohgodwhy

Reputation: 50798

I still think that, regarding my comment, the answer remains the same. If you wish to utilize the docker environment variables as opposed to your local .env variables, then the config directory is still the way to go. In this case, it looks like you wish to target the database host. So let's do that:

In your config/database.php file, change the following:

'mysql' => [
    //...
    'host' => getenv('DB_HOST') ?: env('DB_HOST', 'defaultvalue')
] 

Then only make reference to the host through the config file.

config("database.mysql.host");

You will get the ENV from your docker container if it exists, otherwise you will get the DB_HOST declaration from your .env file.

Upvotes: 1

Related Questions