janiskelemen/laravel-setting

Laravel 设置管理器

2.1.0 2024-09-20 23:53 UTC

README

Total Downloads Build Status

  • 简单的键值存储
  • 支持配置文件作为默认设置
  • 支持多级数组(点分隔的键)结构
  • 支持存储单个用户的设置
  • 支持本地化
  • 设置被缓存

安装

通过 Composer

composer require janiskelemen/laravel-setting

发布配置和迁移

php artisan vendor:publish --tag=setting
php artisan migrate

设置你的配置

发布设置文件后,你将找到一个新的配置文件:config/setting.php 在这个配置中,你可以定义以下基本设置。

# config/setting.php
return [
    'app_name' => 'My Application',
    'user_limit' => 10,
];
Setting::get('app_name');
//retruns
'My Application'

你还可以使用多级数组

# config/setting.php
return [
    'priorities' => [
        'low' => 1,
        'medium' => 2,
        'hight' => 3
    ],
];
Setting::get('priorities.medium');
//retruns
2

定义可选的配置值

如果你想为特定的设置存储额外的数据,你可以使用数组并命名一个参数为 'default_value',这将作为设置的默认值,并且是 Settings::get('app_name') 在这种情况下返回的值。

# config/setting.php
return [
    'app_name' => [
        'type' => 'text', /* Optional config values */
        'max' => 255, /* Optional config values */
        'default_value' => 'My Application' /* <- This value will be returned by Setting::get('app_name') if key is not found in DB */
    ],
    'user_limit' => 10,
];
Setting::get('app_name');
//retruns
'My Application'

// You can still access the optional parameters
Setting::get('app_name.max');
//retruns
255

获取完整的结构,包括 default_value 键

通过在键名后缀添加点或使用 Setting::getWithDefaultSubKeys('app_name') 方法将返回所有默认参数,包括当前值

Setting::get('app_name.');
//retruns
[
    'type' => 'text',
    'max' => 255,
    'default_value' => 'My Application',
    'value' => 'My Custom Application Name' //the value key will be added with the current value saved in the database (or default if not in database yet)
]

作用域设置

你可能只想为某个用户保存一些设置。你可以在配置键名中使用占位符(_)来实现。

# config/setting.php
return [
    'user_*' => [
        'dark_mode' => false,
        'permissions' => [
            'read' => true,
            'write' => false,
        ]
    ],
];

在运行时设置新设置

// Save a new setting under user_1.dark_mode with a value of true
Setting::set("user_{$user->id}.dark_mode", true);

现在你可以获取值

Setting::get("user_{$user->id}.dark_mode");
//returns
true

上述代码如果此用户不存在设置将返回 null。为了返回其他内容,你可以将默认值作为第二个参数设置

Setting::get("user_{$otherUser->id}.dark_mode");
//returns
false

仅获取已更改的用户设置

Setting::set("user_{$otherUser->id}.dark_mode", true);
Setting::set("user_{$otherUser->id}.permissions.write", true);

Setting::get("user_{$otherUser->id}");
//returns
[
    'dark_mode' => true,
    'permissions' => [
        'write' => false
    ]
]

为了获取所有用户设置,你可以使用 getWithDefaultSubKeys() 方法或在主键后缀添加点。结果将返回一个合并的数组,其中包含配置中的默认值和数据库中的更改值,后者将覆盖默认值。

Setting::get("user_{$otherUser->id}.");
// same as
Setting::getWithDefaultSubKeys("user_{$otherUser->id}");

//returns
[
    'dark_mode' => true, // this value comes from the database
    'permissions' => [
        'read' => true, // this value is the default from the config
        'write' => false, // this value is the default from the config
    ]
]

用法

Setting::get('name');
// get setting value with key 'name'
// If this key is not found in DB then it will return the value defined from the config file or null if the key is also not defined in the config file.

Setting::get('name', 'Joe');
// get setting value with key 'name'
// return 'Joe' if the key does not exists. This will overwrite the default coming from the config file.

Setting::all();
// get all settings.
// This will merge the setting.php config file with the values (only where lang is null) found in the database and returns a collection.

Setting::lang('zh-TW')->get('name', 'Joe');
// get setting value with key and language

Setting::set('name', 'Joe');
// set setting value by key

Setting::lang('zh-TW')->set('name', 'Joe');
// set setting value by key and language

Setting::has('name');
// check the key exists in database, return boolean

Setting::lang('zh-TW')->has('name');
// check the key exists by language in database, return boolean

Setting::forget('name');
// delete the setting from database by key

Setting::lang('zh-TW')->forget('name');
// delete the setting from database by key and language

处理区域设置

默认情况下,语言参数在每次设置或获取调用时都会重置。你可以禁用此功能,并使用任何路由服务提供商或其他方法设置自己的长期语言参数。

Setting::lang(App::getLocale())->langResetting(false);

自定义设置模型

可以通过创建 /config/laravel-setting.php 配置并添加来自定义 Setting 模型。

'model' => \App\YourModelName::class,

你的自定义模型需要扩展 \JanisKelemen\Setting\EloquentStorage 类。

变更日志

请参阅 变更日志 了解最近更改的详细信息。

测试

$ composer test

贡献

请参阅 contributing.md 了解详细信息。

安全

如果你发现任何安全问题,请通过 Twitter 发送私信给我 @janiskelemen,而不是使用问题跟踪器。

鸣谢

此包主要是从 UniSharp/laravel-settings 分支出来的。

许可

MIT。请参阅 许可文件 了解更多信息。