简体   繁体   English

在不重启服务器的情况下重新加载 .env 变量(Laravel 5,共享托管)

[英]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:我的 Laravel 5 一直运行正常,直到配置了数据库,然后发现这个错误:

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.做了一些研究,似乎我配置 MySQL 访问太晚了,所以我应该重新启动服务器以获得正确的环境变量。 Well, I'm using Dreamhost's shared server and I just can't do that.好吧,我正在使用 Dreamhost 的共享服务器,但我不能这样做。

How should I fix this issue?我应该如何解决这个问题?

Thanks谢谢

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. 如果您在服务器上运行了php artisan config:cache ,那么您的Laravel应用程序可能会缓存您放入.env文件中的过期配置设置。

Run php artisan config:clear to fix that. 运行php artisan config:clear以解决这个问题。

It's possible that your configuration variables are cached. 您的配置变量可能会被缓存。 Verify your config/app.php as well as your .env file then try 验证您的config/app.php以及.env文件,然后尝试

php artisan cache:clear

on the command line. 在命令行上。

I know this is old, but for local dev, this is what got things back to a production .env file: 我知道这是旧的,但对于本地开发者来说,这就是生产.env文件的原因:

rm bootstrap/cache/config.php

then 然后

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

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 在我的情况下,我需要在以编程方式更改.env后重新建立数据库连接,但它不起作用,如果你遇到这个麻烦试试这个

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. 重新加载.env后,这是因为Laravel App之前可以访问默认连接, \\Illuminate\\Database\\DatabaseManager需要重新读取配置参数。

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. 如果有人偶然发现这个问题谁不能重新加载他们的网络服务器(长时间运行的控制台命令,如队列运行)或需要在请求中重新加载他们的.env文件,我找到了一种方法来正确地重新加载laravel 5中的.env变量。

use Dotenv;
use InvalidArgumentException;

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

To be clear there are 4 types of caches you can clear depending upon your case. 需要说明的是,根据您的情况,您可以清除4种类型的缓存。

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. 它的作用是清除存储\\ 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 这将清除env文件的缓存并重新加载它

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. 大多数共享主机提供程序不提供对系统的SSH访问。 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";
});

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.这是我能找到的唯一一个与在现有应用程序实例上重新加载.envconfig($key)值有关的问题,但其中许多答案可能应该存在于不同的问题上。

I got the following to work for a Laravel 6.x application, while trying to make an artisan command use my cypress environment variables.我在 Laravel 6.x 应用程序中使用了以下内容,同时尝试使 artisan 命令使用我的 cypress 环境变量。

For context $this->laravel === app() .对于上下文$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.对上面的答案大喊大叫,为我指明了正确的方向,但我将在这里包括我的答案,因为它们都不适合我。

In config/database.php I changed the default DB connection from mysql to sqlite. config/database.php我将默认的数据库连接从mysql更改为sqlite。 I deleted the .env file (actually renamed it) and created the sqlite file with touch storage/database.sqlite . 我删除了.env文件(实际上.env其重命名)并使用touch storage/database.sqlite创建了sqlite文件。 The migration worked with sqlite. 迁移适用于sqlite。

Then I switched back the config/database.php default DB connection to mysql and recovered the .env file. 然后我将config/database.php默认数据库连接切换回mysql并恢复.env文件。 The migration worked with mysql. 迁移适用于mysql。

It doesn't make sense I guess. 我觉得这没有意义。 Maybe was something serverside. 也许是服务器的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM