painlesscode / laravel-dynamic-config

一个用于动态控制配置的 Laravel 扩展包

v1.0.1 2020-12-06 14:11 UTC

This package is auto-updated.

Last update: 2024-09-06 22:26:32 UTC


README

PHP Composer test License

简介

本包允许用户将配置存储在数据库中,便于定制。支持缓存以提高访问速度。

安装

您可以通过 composer 安装此包

composer require painlesscode/laravel-dynamic-config

使用以下命令发布配置

php artisan vendor:publish --provider="Painless\DynamicConfig\DynamicConfigServiceProvider"

用法

您只需决定哪些配置文件需要动态编辑,只需将这些文件的名称追加到 dynamic_configs 数组中即可

# /config/dynamic_config.php 
return [
    'dynamic_configs' => [
        'mail',
    ],
];
  • 为了测试目的,已在 dynamic_config 数组中添加了 mail。如果您不需要它,可以自由地将其删除。
  • 默认值将来自实际的配置文件。
  • dynamic_config 添加到 dynamic_configs 数组中没有任何效果。
  • 您可以为更快的访问启用缓存。要启用动态配置的缓存,只需将 dynamic_config.php 文件中的 enable_cache 键值更改为 true
  • 缓存文件将存储在 bootstrap/cache/dynamic_config.php 文件中。您可以通过编辑 dynamic_config.php 文件中的 cache_file_name 键来更改缓存文件名。

获取动态配置值

echo config('mail.default'); 
// Will return the value of dynamic mail.default (if mail is already added to dynamic_configs array);

获取原始配置值

echo config('defailt.mail.default'); 
// Will return the value of original configuration (if default_prefix is set to 'default');

设置动态配置值

config('mail.default', 'array'); 
// It is like default laravel config set. it will be set but persists in only current request.

// to set value permanently
use Painless\DynamicConfig\Facades\DynamicConfig; 

// or you can use DynamicConfig Alias

DynamicConfig::set('mail.default', 'ses'); 
// It will save the value and persist it in database and cache (if enabled)

重置配置值

将配置值重置为其原始状态

use Painless\DynamicConfig\Facades\DynamicConfig; 
// or you can use DynamicConfig Alias

DynamicConfig::revert('mail.default', 'ses'); 
// It will revert the config value to its original state and persist it.