如何在 Laravel 中编辑和保存自定义配置文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25711296/
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
How to edit and save custom config files in Laravel?
提问by scorpion909
I am creating simple web application in Laravel 4. I have backend for managing applications content. As a part of backend i want to have UI to manage applications settings. I want my configuration variables to be stored in file [FOLDER: /app/config/customconfig.php].
我正在 Laravel 4 中创建简单的 Web 应用程序。我有用于管理应用程序内容的后端。作为后端的一部分,我希望有 UI 来管理应用程序设置。我希望我的配置变量存储在文件 [FOLDER: /app/config/customconfig.php] 中。
I was wondering if there's any possibility in Laravel how to have custom config file, which can be managed/updated thru backend UI?
我想知道 Laravel 中是否有可能如何拥有自定义配置文件,该文件可以通过后端 UI 进行管理/更新?
回答by Batman
I did it like this ...
我是这样做的...
config(['YOURKONFIG.YOURKEY' => 'NEW_VALUE']);
$fp = fopen(base_path() .'/config/YOURKONFIG.php' , 'w');
fwrite($fp, '<?php return ' . var_export(config('YOURKONFIG'), true) . ';');
fclose($fp);
回答by Antonio Carlos Ribeiro
You'll have to extend the Fileloader, but it's very simple:
您必须扩展 Fileloader,但这非常简单:
class FileLoader extends \Illuminate\Config\FileLoader
{
public function save($items, $environment, $group, $namespace = null)
{
$path = $this->getPath($namespace);
if (is_null($path))
{
return;
}
$file = (!$environment || ($environment == 'production'))
? "{$path}/{$group}.php"
: "{$path}/{$environment}/{$group}.php";
$this->files->put($file, '<?php return ' . var_export($items, true) . ';');
}
}
Usage:
用法:
$l = new FileLoader(
new Illuminate\Filesystem\Filesystem(),
base_path().'/config'
);
$conf = ['mykey' => 'thevalue'];
$l->save($conf, '', 'customconfig');
回答by Nicholas Vasilaki
Based upon @Batman answer with respect to current version (from 5.1 to 6.x):
基于@Batman 对当前版本的回答(从 5.1 到 6.x):
config(['YOUR-CONFIG.YOUR_KEY' => 'NEW_VALUE']);
$text = '<?php return ' . var_export(config('YOUR-CONFIG'), true) . ';';
file_put_contents(config_path('YOUR-CONFIG.php'), $text);
回答by passioncoder
Afiak there is no built-in functionality for manipulating config files. I see 2 options to achieve this:
Afiak 没有用于操作配置文件的内置功能。我看到 2 个选项来实现这一目标:
- You can store your custom config in your database and override the default config at runtime with
Config::set('key', 'value');
But be aware that
- 您可以将自定义配置存储在数据库中并在运行时覆盖默认配置,
Config::set('key', 'value');
但请注意
Configuration values that are set at run-time are only set for the current request, and will not be carried over to subsequent requests. @see: http://laravel.com/docs/configuration
在运行时设置的配置值仅针对当前请求设置,不会被带到后续请求中。@see:http: //laravel.com/docs/configuration
- Since config files are simple php arrays, it's easy to read, manipulate and writethem. So with a little custom code this should be done quickly.
- 由于配置文件是简单的 php 数组,因此很容易阅读、操作和编写它们。因此,使用一些自定义代码应该可以快速完成。
In general I'd prefer the first option. Overriding config files can might cause some troubles when it comes to version control, deployment, automated testing, etc. But as always, this strongly depends on your project setup.
一般来说,我更喜欢第一个选项。在版本控制、部署、自动化测试等方面,覆盖配置文件可能会导致一些麻烦。但与往常一样,这在很大程度上取决于您的项目设置。