如何在 laravel 中添加和更新配置变量
How to add and update config varaibles in laravel
我有一个配置变量文件,我在我的控制器中使用它而不是 mysql 以获得更快的性能。
但我的问题是我只能读取此配置文件,无法添加或更新其中的任何值。
关于如何更新或向该变量文件添加新值的任何建议:
我的变量文件存储在/config/Banners_size.php
:
return [
"normal_x970h90" => [
'status' => 'enable',
'value' => '500'
],
"normal_x234h60" => [
'status' => 'enable',
'value' => '500'
],
]
我的 php 用于向其中添加新数组但不起作用的代码:
$banners = Config('Banners_size');
$banner = array(
$request->input('size') => [
'status' => $request->input('status'),
'value' => $request->input('cost')
]
);
$bannerinfo = array_merge($banners, $banner);
Config('Banners_size' , $bannerinfo);
配置文件是只读文件,不能以编程方式向其添加参数。
$banners = config('Banners_size');
config('Banners_size' , $bannerinfo);
以上两行,作用相同,都是return Banner_size
.
的值
要在运行时设置配置值,将数组传递给配置助手:
config(['app.timezone' => 'America/Chicago']);
注意 - 它不会为下一个请求保留数据
从技术上讲你可以...
//edit config for current runtime
config(['FILE.KEY' => 'NEW_VALUE']);
// open config file for writing
$fp = fopen(base_path() .'/config/FILE.php' , 'w');
// write updated runtime config to file
fwrite($fp, '<?php return ' . var_export(config('FILE'), true) . ';');
// close the file
fclose($fp);
// clear config cache
Artisan::call('cache:clear');
<?php
namespace App\Helpers;
class Uid
{
// You can simply use this function :
// you have to stick to using single quotation (')
public static function setConfig($config,$key,$value,$path)
{
file_put_contents(
$path,
str_replace("'$key'". ' => ' ."'" . config($config)."'",
"'$key'". ' => ' ."'" . $value ."'",
file_get_contents($path))
);
// clear config cache
Artisan::call('cache:clear');
}
// Or use these. It makes no difference if you use a single or double quotation
public static function setConfig($config,$key,$value,$path)
{
file_put_contents(
$path,
str_replace(["'$key'". ' => ' ."'" . config($config)."'","\"$key\"". ' => ' ."\"" . config($config)."\""],
"'$key'". ' => ' ."'" . $value ."'",
file_get_contents($path))
);
// clear config cache
Artisan::call('cache:clear');
}
/*
* $name => Config Name
* $key => The key whose value you want to change; Example : 'redis.client'
* $value => The new value, you can add an array
* $implodeCharacter => spacers between keys
*/
public static function putConfig($name,$key,$value,$implodeCharacter = '.' )
{
$path = base_path('config/'.$name.'.php');
$config = config($name);
$data = self::findAndReplace($key, $config,$value,$implodeCharacter);
file_put_contents($path,'<?php
return '.var_export($data,true).';');
Artisan::call('cache:clear');
}
public static function findAndReplace($key, $config,$value,$implodeCharacter) {
$parts = explode($implodeCharacter, $key);
for($i=0;$i < count($parts);$i++) {
if($i == count($parts)-1){
$config[$parts[$i]] = $value;
}else{
$config= $config[$parts[$i]];
}
}
return $config;
}
}
我有一个配置变量文件,我在我的控制器中使用它而不是 mysql 以获得更快的性能。
但我的问题是我只能读取此配置文件,无法添加或更新其中的任何值。
关于如何更新或向该变量文件添加新值的任何建议:
我的变量文件存储在/config/Banners_size.php
:
return [
"normal_x970h90" => [
'status' => 'enable',
'value' => '500'
],
"normal_x234h60" => [
'status' => 'enable',
'value' => '500'
],
]
我的 php 用于向其中添加新数组但不起作用的代码:
$banners = Config('Banners_size');
$banner = array(
$request->input('size') => [
'status' => $request->input('status'),
'value' => $request->input('cost')
]
);
$bannerinfo = array_merge($banners, $banner);
Config('Banners_size' , $bannerinfo);
配置文件是只读文件,不能以编程方式向其添加参数。
$banners = config('Banners_size');
config('Banners_size' , $bannerinfo);
以上两行,作用相同,都是return Banner_size
.
要在运行时设置配置值,将数组传递给配置助手:
config(['app.timezone' => 'America/Chicago']);
注意 - 它不会为下一个请求保留数据
从技术上讲你可以...
//edit config for current runtime
config(['FILE.KEY' => 'NEW_VALUE']);
// open config file for writing
$fp = fopen(base_path() .'/config/FILE.php' , 'w');
// write updated runtime config to file
fwrite($fp, '<?php return ' . var_export(config('FILE'), true) . ';');
// close the file
fclose($fp);
// clear config cache
Artisan::call('cache:clear');
<?php
namespace App\Helpers;
class Uid
{
// You can simply use this function :
// you have to stick to using single quotation (')
public static function setConfig($config,$key,$value,$path)
{
file_put_contents(
$path,
str_replace("'$key'". ' => ' ."'" . config($config)."'",
"'$key'". ' => ' ."'" . $value ."'",
file_get_contents($path))
);
// clear config cache
Artisan::call('cache:clear');
}
// Or use these. It makes no difference if you use a single or double quotation
public static function setConfig($config,$key,$value,$path)
{
file_put_contents(
$path,
str_replace(["'$key'". ' => ' ."'" . config($config)."'","\"$key\"". ' => ' ."\"" . config($config)."\""],
"'$key'". ' => ' ."'" . $value ."'",
file_get_contents($path))
);
// clear config cache
Artisan::call('cache:clear');
}
/*
* $name => Config Name
* $key => The key whose value you want to change; Example : 'redis.client'
* $value => The new value, you can add an array
* $implodeCharacter => spacers between keys
*/
public static function putConfig($name,$key,$value,$implodeCharacter = '.' )
{
$path = base_path('config/'.$name.'.php');
$config = config($name);
$data = self::findAndReplace($key, $config,$value,$implodeCharacter);
file_put_contents($path,'<?php
return '.var_export($data,true).';');
Artisan::call('cache:clear');
}
public static function findAndReplace($key, $config,$value,$implodeCharacter) {
$parts = explode($implodeCharacter, $key);
for($i=0;$i < count($parts);$i++) {
if($i == count($parts)-1){
$config[$parts[$i]] = $value;
}else{
$config= $config[$parts[$i]];
}
}
return $config;
}
}