sewidan / laravel-setting
Laravel 设置管理器
Requires
- adbario/php-dot-notation: ^3.1.1
- illuminate/support: *
Requires (Dev)
- mockery/mockery: ^1.5
- orchestra/database: dev-master
- orchestra/testbench: ~7.0
- phpunit/phpunit: ^9.0
- sempro/phpunit-pretty-print: ^1.4
This package is auto-updated.
Last update: 2024-09-08 14:58:37 UTC
README
- 简单的键值存储
- 支持配置文件以支持默认设置
- 支持多级数组(点分隔的键)结构
- 支持存储单个用户设置
- 支持本地化
- 设置被缓存
安装
通过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配置文件并添加以下内容来覆盖设置模型
'model' => \App\YourModelName::class,
您的自定义模型需要继承自 \JanisKelemen\Setting\EloquentStorage 类。
更改日志
请参阅changelog以获取有关最近更改的更多信息。
测试
$ composer test
贡献
请参阅contributing.md以获取详细信息。
安全性
如果您发现任何安全问题,请通过Twitter私信@janiskelemen告诉我,而不是使用问题跟踪器。
鸣谢
此包主要基于UniSharp/laravel-settings的分支。
许可
MIT。有关更多信息,请参阅许可文件。