我想创建一个Laravel Web应用程序,允许管理员用户使用Web后端系统更改.env文件中的某些变量(如数据库凭据).但是我如何保存更改呢?

推荐答案

没有内在的方法可以做到这一点.如果您真的想更改.env文件的内容,那么必须结合使用某种字符串替换和PHP的文件编写方法.要获得一些灵感,您应该看看key:generate命令:KeyGenerateCommand.php:

$path = base_path('.env');

if (file_exists($path)) {
    file_put_contents($path, str_replace(
        'APP_KEY='.$this->laravel['config']['app.key'], 'APP_KEY='.$key, file_get_contents($path)
    ));
}

After the file path is built and the existence is checked, the command simply replaces APP_KEY=[current app key] with APP_KEY=[new app key]. You should be able to do the same string replacement with other variables.
Last but not least I just wanted to say that it might isn't the best idea to let users change the .env file. For most custom settings I would recommend storing them in the database, however this is obviously a problem if the setting itself is necessary to connect to the database.

Laravel相关问答推荐

Laravel Eloquent:组合列(或自定义属性)上的 Where 子句

我的共享主机上的每个帖子请求都出现 503 服务不可用

从主类别模型 laravel 中获取子类别翻译

前端和管理员分开时未加载laravel 9中间件

(1/1) ErrorException ReflectionFunction::__construct() ex

Laravel中具有两个外键字段的数据库一对多

Eloquent 的集合方法,例如 only 或 except 返回一个空集合

调用未定义的函数 App\Http\Controllers\ [函数名]

如何在 Laravel 5.5 中扩展 vendor 包服务提供者

如何修复无效请求(不支持的 SSL 请求)

Laravel - 超过 PHP 最大上传大小限制时验证文件大小

找不到列:1054字段列表中的未知列0-Laravel-我的代码中的任何地方都没有0列

Laravel上传文件无法写入目录

laravel 搜索多个以空格分隔的单词

使用 Laravel 将两个模型合并到一个分页查询中,并带有Eager加载的关系

如何处理 php (Laravel api) 和 javascript (AngularJS) 之间的日期时间

如果用户未登录 Laravel,则重定向到登录

在 Laravel 5.4 中将文件存储在公共目录和存储中的区别

Eloquent - 带有字符串值而不是列标题的连接子句

基于日期的 Laravel 日志(log)文件