我有一个自定义CMS,我正在用Laravel从头开始编写,希望在用户设置后从控制器设置env个值,即数据库详细信息、邮件详细信息、一般配置等,并希望给用户灵活性,使用我正在制作的GUI在移动中更改它们.

So my question is how do I write the values received from user to the .env file as an when I need from the controller.

And is it a good idea to build the .env file on the go or is there any other way around it?

提前谢谢您.

推荐答案

Since Laravel uses config files to access and store .env data, you can set this data on the fly with config() method:

config(['database.connections.mysql.host' => '127.0.0.1']);

To get this data use config():

config('database.connections.mysql.host')

To set configuration values at runtime, pass an array to the config helper

https://laravel.com/docs/5.3/configuration#accessing-configuration-values

Laravel相关问答推荐

Laravel 10 - 没有 $append 属性的Eloquent 的热切加载

通过 laravel 中的 url 传递多个字符串作为参数以从数据库中删除特定数据

Laravel 模型事件:delete() 不会从存储中删除文件

验证错误后的 Laravel 自定义重定向

Laravel 5 - 更改模型文件位置

Laravel 有Many Many to Many To One Eloquent

Laravel Blade 模板 @foreach 订单

Laravel 5.3 创建模型返回字段没有默认值

如何在 laravel 中使用 str_replace

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

laravel:如何在 Eloquent Query 中使用 LOWERCASE 函数?

如何从 laravel 控制器执行外部 shell 命令?

为什么人们将 .env 放入 gitignore?

laravel 预期响应代码 250 但得到代码530

如何卸载 Laravel?

为什么客户凭证应该与 Laravel Passport 中的用户相关联?

如何从不是控制器方法的方法发送响应?

Laravel 5 - 从控制器级别的所有请求对象中删除参数

[Vue 警告]:找不到元素:#app

Laravel Route 模型与关系绑定