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

前端 未结 7 2129
旧巷少年郎
旧巷少年郎 2020-12-23 13:46

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

SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name o         


        
相关标签:
7条回答
  • 2020-12-23 14:22

    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.

    0 讨论(0)
  • 2020-12-23 14:23

    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.

    0 讨论(0)
  • 2020-12-23 14:28

    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) {
        //
    }
    
    0 讨论(0)
  • 2020-12-23 14:31

    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
    
    0 讨论(0)
  • 2020-12-23 14:34

    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";
    });
    
    0 讨论(0)
  • 2020-12-23 14:39

    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.

    0 讨论(0)
提交回复
热议问题