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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32423034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:58:12  来源:igfitidea点击:

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

phpmysqllaravellaravel-5dreamhost

提问by godsaway

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. Well, I'm using Dreamhost's shared server and I just can't do that.

做了一些研究,似乎我配置 MySQL 访问太晚了,所以我应该重新启动服务器以获得正确的环境变量。好吧,我正在使用 Dreamhost 的共享服务器,但我不能这样做。

How should I fix this issue?

我应该如何解决这个问题?

Thanks

谢谢

采纳答案by godsaway

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

config/database.php我将默认数据库连接从 mysql 更改为 sqlite。我删除了该.env文件(实际上是重命名了它)并创建了 .sqlite 文件touch storage/database.sqlite。迁移与 sqlite 一起使用。

Then I switched back the config/database.phpdefault DB connection to mysql and recovered the .envfile. The migration worked with mysql.

然后我将config/database.php默认的数据库连接切换回mysql 并恢复了.env文件。迁移与 mysql 一起工作。

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

我想这没有意义。也许是服务器端的东西。

回答by Harry

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

如果您php artisan config:cache在服务器上运行,那么您的 Laravel 应用程序可能会缓存您放入.env文件中的过时配置设置。

Run php artisan config:clearto fix that.

运行php artisan config:clear以解决该问题。

回答by AurelienT

It's possible that your configuration variables are cached. Verify your config/app.phpas well as your .envfile then try

您的配置变量可能已被缓存。验证您config/app.php和您的.env文件,然后尝试

php artisan cache:clear

on the command line.

在命令行上。

回答by Janiis

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

回答by MatteoOreficeIT

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\DatabaseManagerneeds to re-read config parameters.

重新加载 .env 后,那是因为 Laravel App 之前可能已经访问了默认连接,并且\Illuminate\Database\DatabaseManager需要重新读取配置参数。

回答by Dibyendu Mitra Roy

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.

当您希望清除应用程序缓存时,您可以在控制台中运行上述语句。它的作用是清除 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

这将清除 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. In such a case you will need to create a route and call the following line as below:

大多数共享主机提供商不提供对系统的 SSH 访问。在这种情况下,您需要创建一个路由并调用以下行:

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

回答by Question Mark

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) {
    //
}