godsaway
godsaway

Reputation: 671

Reloading .env variables without restarting server (Laravel 5, shared hosting)

My Laravel 5 has run OK until the database was configured, then found this error:

SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known

Doing some research it seems that I configured MySQL access too late, so I should restart the server in order to get the correct environment variables. Well, I'm using Dreamhost's shared server and I just can't do that.

How should I fix this issue?

Thanks

Upvotes: 60

Views: 224282

Answers (9)

madbob
madbob

Reputation: 640

A variant of existing answer, including DB reload, tested and working on Laravel 10:

use Illuminate\Support\Env;
use Illuminate\Foundation\Bootstrap\LoadConfiguration;
use Dotenv\Dotenv;

$start_connection = env('DB_CONNECTION');
app()->loadEnvironmentFrom('.env.myotherenv');
Dotenv::create(Env::getRepository(), app()->environmentPath(), app()->environmentFile())->load();
(new LoadConfiguration())->bootstrap(app());
app('db')->purge($start_connection);

Here more details.

Upvotes: 0

Kurt Friars
Kurt Friars

Reputation: 3764

This is the only question I could find relating to reloading .env and thus config($key) values on an existing app instance, but many of these answers should probably live on a different question.

I got the following to work for a Laravel 6.x application, while trying to make an artisan command use my cypress environment variables.

For context $this->laravel === app().

    /**
     * Make the application use the cypress environment variables
     *
     * @return void
     */
    protected function enableCypressEnv()
    {
        // Get all of the original values from config. We need to do this because
        // rebuilding config will skip packages.
        $old = app(Repository::class)->all();

        // Change the applications env file
        $this->laravel->loadEnvironmentFrom('.env.cypress');

        // Reload the app's environment variables 
        Dotenv::create(
            $this->laravel->environmentPath(),
            $this->laravel->environmentFile(),
            Env::getFactory()
        )->overload();

        // Force config to be rebuitl with new env
        (new LoadConfiguration())->bootstrap($this->laravel);
        
        // Get all of the new values from buidling with new env
        $new = app(Repository::class)->all();

        // Merge the new values over the old distinctly
        $merged = array_merge_recursive_distinct($old, $new);

        // Get the applications configuration instance
        $config = config();

        // Overwrite all of the values in the container in accordance with the merged values
        foreach ($merged as $key => $value) {
            $config->set([$key => $value]);
        }
    }

Shoutout to the answers above for pointing me in the right direction, but I will include my answer here as none of them worked perfectly for me.

Upvotes: 1

Janiis
Janiis

Reputation: 1576

I know this is old, but for local dev, this is what got things back to a production .env file:

rm bootstrap/cache/config.php

then

php artisan config:cache
php artisan config:clear
php artisan cache:clear

Upvotes: 41

Dibyendu Mitra Roy
Dibyendu Mitra Roy

Reputation: 1665

To be clear there are 4 types of caches you can clear depending upon your case.

php artisan cache:clear

You can run the above statement in your console when you wish to clear the application cache. What it does is that this statement clears all caches inside storage\framework\cache.

php artisan route:cache

This clears your route cache. So if you have added a new route or have changed a route controller or action you can use this one to reload the same.

php artisan config:cache 

This will clear the caching of the env file and reload it

php artisan view:clear 

This will clear the compiled view files of your application.

For Shared Hosting

Most of the shared hosting providers don't provide SSH access to the systems. In such a case you will need to create a route and call the following line as below:

Route::get('/clear-cache', function() {
    Artisan::call('cache:clear');
    return "All cache cleared";
});

Upvotes: 18

MatteoOreficeIT
MatteoOreficeIT

Reputation: 199

A short solution:

use Dotenv;

with(new Dotenv(app()->environmentPath(), app()->environmentFile()))->overload();
with(new LoadConfiguration())->bootstrap(app());

In my case I needed to re-establish database connection after altering .env programmatically, but it didn't work , If you get into this trouble try this

app('db')->purge($connection->getName()); 

after reloading .env , that's because Laravel App could have accessed the default connection before and the \Illuminate\Database\DatabaseManager needs to re-read config parameters.

Upvotes: 7

Question Mark
Question Mark

Reputation: 3606

In case anybody stumbles upon this question who cannot reload their webserver (long running console command like a queue runner) or needs to reload their .env file mid-request, i found a way to properly reload .env variables in laravel 5.

use Dotenv;
use InvalidArgumentException;

try {
    Dotenv::makeMutable();
    Dotenv::load(app()->environmentPath(), app()->environmentFile());
    Dotenv::makeImmutable();
} catch (InvalidArgumentException $e) {
    //
}

Upvotes: 3

Harry
Harry

Reputation: 2546

If you have run php artisan config:cache on your server, then your Laravel app could cache outdated config settings that you've put in the .env file.

Run php artisan config:clear to fix that.

Upvotes: 122

godsaway
godsaway

Reputation: 671

In config/database.php I changed the default DB connection from mysql to sqlite. I deleted the .env file (actually renamed it) and created the sqlite file with touch storage/database.sqlite. The migration worked with sqlite.

Then I switched back the config/database.php default DB connection to mysql and recovered the .env file. The migration worked with mysql.

It doesn't make sense I guess. Maybe was something serverside.

Upvotes: -11

AurelienT
AurelienT

Reputation: 351

It's possible that your configuration variables are cached. Verify your config/app.php as well as your .env file then try

php artisan cache:clear

on the command line.

Upvotes: 20

Related Questions