本文目录导读:

在 Laravel 中,配置动态刷新主要涉及缓存配置的清除和重新加载,以下是几种常见的方法:
清除配置缓存
Artisan 命令方式
# 清除配置缓存 php artisan config:clear # 重新生成配置缓存 php artisan config:cache
在代码中执行
use Illuminate\Support\Facades\Artisan;
// 清除配置缓存
Artisan::call('config:clear');
// 重新缓存配置
Artisan::call('config:cache');
运行时动态修改配置
使用 Config Facade
use Illuminate\Support\Facades\Config;
// 读取配置
$value = Config::get('app.name');
// 动态设置配置(仅当前请求有效)
Config::set('app.timezone', 'Asia/Shanghai');
// 批量设置
Config::set([
'app.timezone' => 'Asia/Shanghai',
'app.locale' => 'zh-CN'
]);
使用全局 helper 函数
// 读取配置
$value = config('app.name');
// 设置配置(仅当前请求有效)
config(['app.timezone' => 'Asia/Shanghai']);
数据库驱动的动态配置
创建动态配置系统:
创建配置表 Migration
// database/migrations/create_dynamic_configs_table.php
Schema::create('dynamic_configs', function ($table) {
$table->id();
$table->string('key')->unique();
$table->text('value')->nullable();
$table->string('type')->default('string');
$table->timestamps();
});
创建 Service Provider
// app/Providers/DynamicConfigServiceProvider.php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Config;
class DynamicConfigServiceProvider extends ServiceProvider
{
public function boot()
{
if (app()->runningInConsole()) {
return;
}
// 加载动态配置
$this->loadDynamicConfigs();
}
protected function loadDynamicConfigs()
{
try {
$configs = \App\Models\DynamicConfig::all();
foreach ($configs as $config) {
// 将点号转换为数组访问
Config::set($config->key, $this->parseValue($config));
}
} catch (\Exception $e) {
// 数据库不存在时的处理
}
}
protected function parseValue($config)
{
switch ($config->type) {
case 'boolean':
return filter_var($config->value, FILTER_VALIDATE_BOOLEAN);
case 'integer':
return (int) $config->value;
case 'json':
return json_decode($config->value, true);
default:
return $config->value;
}
}
}
创建配置 Model
// app/Models/DynamicConfig.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
class DynamicConfig extends Model
{
protected $fillable = ['key', 'value', 'type'];
// 更新配置时清除缓存
protected static function booted()
{
static::saved(function () {
Cache::forget('dynamic_configs');
});
static::deleted(function () {
Cache::forget('dynamic_configs');
});
}
// 获取所有配置(带缓存)
public static function getAllCached()
{
return Cache::remember('dynamic_configs', 3600, function () {
return self::all();
});
}
}
辅助类
// app/Helpers/DynamicConfig.php
namespace App\Helpers;
use App\Models\DynamicConfig as ConfigModel;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Config;
class DynamicConfigHelper
{
public static function get($key, $default = null)
{
$config = ConfigModel::where('key', $key)->first();
if (!$config) {
return $default;
}
return self::parseValue($config);
}
public static function set($key, $value, $type = 'string')
{
// 如果是数组或对象,转换为 JSON
if (is_array($value) || is_object($value)) {
$type = 'json';
$value = json_encode($value);
}
// 更新或创建配置
ConfigModel::updateOrCreate(
['key' => $key],
['value' => $value, 'type' => $type]
);
// 刷新运行时配置
Config::set($key, self::parseValue((object)['value' => $value, 'type' => $type]));
// 清除缓存
Cache::forget('dynamic_configs');
}
protected static function parseValue($config)
{
switch ($config->type ?? 'string') {
case 'boolean':
return filter_var($config->value, FILTER_VALIDATE_BOOLEAN);
case 'integer':
return (int) $config->value;
case 'json':
return json_decode($config->value, true);
default:
return $config->value;
}
}
}
使用环境变量动态刷新
use Dotenv\Dotenv;
class EnvConfigRefresh
{
public function refresh()
{
// 重新加载 .env 文件
$dotenv = Dotenv::createImmutable(base_path());
$dotenv->load();
// 强制重新加载环境变量
foreach ($_ENV as $key => $value) {
putenv("$key=$value");
}
}
}
配置文件的动态包含
// config/dynamic.php
return [
'settings' => function() {
// 动态生成配置
return [
'maintenance_mode' => cache('maintenance_mode', false),
'api_rate_limit' => app('App\Services\ConfigService')->getApiRateLimit(),
];
}
];
// 使用方式
$settings = config('dynamic.settings')();
最佳实践建议
缓存策略
// 使用缓存装饰器
class CachedDynamicConfig
{
protected $cacheTTL = 3600; // 1小时
public function get($key, $default = null)
{
return Cache::remember("config:{$key}", $this->cacheTTL, function () use ($key, $default) {
return Config::get($key, $default);
});
}
public function refresh($key = null)
{
if ($key) {
Cache::forget("config:{$key}");
} else {
// 清除所有配置缓存
// 需要根据实际缓存键模式处理
}
}
}
事件驱动刷新
// app/Events/ConfigUpdated.php
class ConfigUpdated
{
public $key;
public $value;
}
// 监听器
class RefreshConfigCache
{
public function handle(ConfigUpdated $event)
{
Cache::forget("config:{$event->key}");
Config::set($event->key, $event->value);
}
}
注意事项
Config::set()仅对当前请求有效,页面刷新后失效- 缓存配置不会自动更新,需要手动清除
- 生产环境谨慎使用
config:cache,因为会导致动态配置失效 - 数据库配置应考虑缓存,避免每次请求都查询数据库
- 对于频繁变更的配置,建议使用缓存 + 事件驱动的方式
根据你的具体需求选择合适的方法,常见的做法是使用数据库 + 缓存的组合方案,配合事件机制实现配置的动态刷新。