PHP code example of wiseadmin / wise-config
1. Go to this page and download the library: Download wiseadmin/wise-config library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
wiseadmin / wise-config example snippets
use wise\config\facade\WiseConfig;
// 读取配置
$siteName = WiseConfig::get('site_name', '默认名称');
// 设置配置
WiseConfig::set('site_name', '我的网站', [
'type' => 'text',
'group' => 'system',
'title' => '站点名称',
]);
// 批量获取
$configs = WiseConfig::many(['site_name', 'site_desc', 'admin_email']);
// 获取分组所有配置
$mailConfigs = WiseConfig::getGroup('mail');
// 检查键是否存在
if (WiseConfig::has('site_name')) {
// ...
}
// 删除配置
WiseConfig::delete('old_config_key');
// 批量设置
WiseConfig::setMany([
'site_name' => ['value' => '新站点', 'type' => 'text', 'group' => 'system'],
'max_upload' => ['value' => '10', 'type' => 'number', 'group' => 'upload'],
]);
// 获取当前 namespace 全部配置
$allConfigs = WiseConfig::all();
use wise\config\facade\WiseConfig;
// 方式一:链式指定 namespace
$value = WiseConfig::namespace('project_a')->get('site_name');
// 方式二:路径中指定 namespace
$value = WiseConfig::get('project_a:system:site_name');
// namespace:group:key — 三段完整路径
// group:key — namespace 自动推断,默认值为 'default'
// key — namespace 自动推断,group 默认为 'system'
// 获取最终生效值(自动沿继承链向上查找)
$seoTitle = WiseConfig::namespace('project_a.plugin_mail')
->getResolved('seo:title', '默认标题');
// 链解析路径:project_a.plugin_mail → project_a → default
// 查看配置来源
$status = WiseConfig::namespace('project_a.plugin_mail')
->getOverrideStatus('seo:title');
// 返回:['exists' => true, 'value' => '...', 'source_ns' => 'project_a', 'is_inherited' => true]
// 删除当前 namespace 的覆盖值,恢复继承上级
WiseConfig::namespace('project_a.plugin_mail')->reset('seo:title');
// 为配置添加标签
WiseConfig::addTag('site_name', 'seo');
// 移除标签
WiseConfig::removeTag('site_name', 'seo');
// 按标签查询配置(AND 逻辑)
$seoConfigs = WiseConfig::whereTags(['seo', 'meta'], 'and');
// 按标签查询配置(OR 逻辑)
$relatedConfigs = WiseConfig::whereTags(['seo', 'meta'], 'or');
use wise\config\bridge\TpConfigBridge;
$bridge = app('wise.config.tp_config_bridge');
$bridge->injectToConfig('default', 'wise');
// 之后可通过 ThinkPHP 方式访问
echo config('wise.system.site_name'); // 站点名称
echo config('wise.upload.max_size'); // 上传限制
// 获取历史版本列表
$histories = WiseConfig::getHistory('site_name');
// 获取指定历史记录
$history = WiseConfig::getHistoryById(1);
// 回滚到指定版本
WiseConfig::revert($historyId);
// 对比两个版本差异
$diff = WiseConfig::diff($historyIdA, $historyIdB);
// 返回:['old' => ..., 'new' => ..., 'diff' => ..., 'is_json' => bool]
// 清除指定键缓存
WiseConfig::clearCacheForKey('site_name');
// 清除分组缓存
WiseConfig::clearCacheForGroup('system');
// 清除全部缓存
WiseConfig::clearAllCache();
use wise\config\facade\WiseDictType;
use wise\config\facade\WiseDictItem;
// 字典项查询
$map = WiseDictItem::getValueLabelMap('order_status'); // ['1' => '待支付', '2' => '已支付', ...]
$label = WiseDictItem::getLabel('order_status', '1'); // '待支付'
$items = WiseDictItem::getItems('order_status'); // 全部字典项
$items = WiseDictItem::getItems('order_status', 1); // 仅启用项
$tree = WiseDictItem::getTree('region'); // 树形结构
// 字典项写入
WiseDictItem::createItem(['dict_code' => 'order_status', 'item_value' => '4', 'item_label' => '已完成']);
WiseDictItem::updateItem(1, ['item_label' => '更新后标签']);
WiseDictItem::deleteItem(1);
WiseDictItem::batchUpdateStatus([1, 2, 3], 1);
WiseDictItem::batchDelete([1, 2]);
// 字典类型
$type = WiseDictType::getType('order_status');
$page = WiseDictType::getList(['name' => '订单']);
WiseDictType::createType(['dict_code' => 'pay_type', 'name' => '支付方式']);
WiseDictType::updateType('pay_type', ['name' => '支付方式(新)']);
WiseDictType::toggleStatus('pay_type');
WiseDictType::deleteType('pay_type');
use wise\config\DictSDK;
// 获取 service 实例(容器就绪时走容器,否则自行构造)
$itemService = DictSDK::getItemService();
// 缓存清理(当前仍由 DictSDK / DictCacheManager 提供)
DictSDK::clearCache('order_status');
DictSDK::clearAllCache();
// 测试用重置内部单例
DictSDK::resetInstances();
use wise\config\service\DictItemService;
$service = app(DictItemService::class);
// 创建字典项
$item = $service->createItem([
'dict_code' => 'order_status',
'item_value' => '4',
'item_label' => '已完成',
'sort' => 4,
]);
// 更新字典项
$service->updateItem(1, ['item_label' => '更新后标签']);
// 分页查询
$list = $service->getList('order_status', [
'page' => 1,
'limit' => 15,
]);
// 批量操作
$service->batchUpdateStatus([1, 2, 3], 1); // 批量启用
$service->batchDelete([1, 2]); // 批量删除
// 删除字典项
$service->deleteItem(1);
use wise\config\contract\TypeHandlerInterface;
class ColorHandler implements TypeHandlerInterface
{
public function getType(): string
{
return 'color';
}
public function decode($rawValue)
{
return '#' . $rawValue;
}
public function encode($inputValue): string
{
return ltrim($inputValue, '#');
}
public function renderFormField(string $name, $value, array $config): string
{
return '<input type="color" name="' . $name . '" value="' . $value . '">';
}
}
// 注册
$manager = app(\wise\config\ConfigManager::class);
$manager->registerTypeHandler(new ColorHandler());
// app/event.php
use wise\config\event\ConfigSaved;
use wise\config\event\DictChangedEvent;
return [
'listen' => [
ConfigSaved::class => [
\app\listener\ConfigChangeListener::class,
],
DictChangedEvent::class => [
\app\listener\DictChangeListener::class,
],
],
];
return [
// 缓存键前缀
'cache_key_prefix' => 'wise_config:',
// 后台路由前缀
'route_prefix' => 'wise-admin/config',
// 图片上传磁盘
'upload_disk' => 'public',
// 图片上传路径前缀
'upload_path' => 'uploads/config/',
// 缓存标签
'cache_tag' => 'wise_config',
// 缓存有效期(秒),0 表示永久
'cache_ttl' => 0,
// 空值缓存 TTL(秒),防穿透
'cache_null_ttl' => 3600,
// 读取时是否仅返回公开(visibility=1)配置(默认 false,服务端可读取私有配置)
'filter_private_on_read' => false,
// 权限验证回调,返回 bool
'auth_callback' => null,
// 未配置 auth_callback 时是否放行(默认 false,即默认拒绝)
'auth_allow_when_unconfigured' => false,
// HTML XSS 过滤
'html_filter' => true,
// 图片上传扩展名限制
'image_extensions' => 'jpg,jpeg,png,gif,webp,bmp',
// 图片上传最大大小(KB)
'image_max_size' => 2048,
// ========== 字典模块 ==========
'dict' => [
'cache_ttl' => 86400,
],
// ========== 命名空间 ==========
'namespace' => [
'default' => 'default',
'auto_resolve' => true,
// 是否允许请求头 X-WiseConfig-Namespace 推断命名空间(默认关闭)
'allow_request_header' => false,
// 命名空间白名单(非空时仅放行其中的值)
'allowed' => [],
],
// ========== 继承 ==========
'inheritance' => [
'enabled' => true,
'max_depth' => 5,
'cache_ttl' => 300,
],
// ========== 标签 ==========
'tag' => [
'enabled' => true,
'max_per_config' => 10,
],
// ========== TP Config 整合 ==========
'tp_integration' => [
'inject_prefix' => 'wise',
],
];
// 在 config/wise-config.php 中配置
'auth_callback' => function () {
return \app\admin\facade\CurrentAdmin::isLogin();
},
bash
php think vendor:publish
bash
php think migrate:run