ubmvn/laravel-settings
Composer 安装命令:
composer require ubmvn/laravel-settings
包简介
Laravel Application & Models Settings
关键字:
README 文档
README
This package allows you to store application wide or model specific Laravel settings in your database. Settings values are casted and stored properly. You can also define your own casts and store repository.
This package fork from https://github.com/iamohd/laravel-settings for Laravel 8.x. Thank Mohammed Isa.
Installation
Install the package via composer:
composer require ubmvn/laravel-settings
Publish the config file with:
php artisan vendor:publish --provider="Ubmvn\Settings\SettingsServiceProvider" --tag="config"
Publish the migration file with:
php artisan vendor:publish --provider="Ubmvn\Settings\SettingsServiceProvider" --tag="migration"
And finally run the migration with:
php artisan migrate
Usage
The package provides various APIs to access the settings manager. You can access it with Settings facade, settings() helper method and via HasSettings trait on your models.
Store Settings
You can set single entry, or multiple entries of settings. The values of objects will be casted according to the rules defined in settings.php config file.
- One Entry
Settings::set('key', 'value');
- Multiple Entries
You can set multiple settings entries by passing an associative array of keys and values. Casts will be applied on all the payload, even on nested arrays.
Settings::set([ 'k1' => Carbon::now(), 'k2' => [ 'k3' => $user, 'k4' => [ 'k5' => $model ], ] ]);
- Specify Settings Group
It's possible to categorize your settings into groups by calling group method.
Settings::group('name')->set('key', 'value'); Settings::group('name')->set([ 'k1' => 'v1', 'k2' => 'v2', ]);
- Model Specific Settings
It's also possible to set settings for a specific model by calling for method
Settings::for($model)->set('key', 'value'); Settings::for($model)->set([ 'k1' => 'v1', 'k2' => 'v2', ]);
- Mixing Filters
You can mix all filters together like this:
Settings::for($model)->group('name')->set('key', 'value');
Retrieve Settings
You can retrieve one or multiple entries and specify the default value if not exist.
- One Entry
Settings::get('k1', 'default');
- Multiple Entries
If the entry key does not exist, the default value will be placed instead
Settings::get(['k1', 'k2', 'k3'], 'default');
- All Entries
If you want to retrieve all entries, you simply call all method. You can also specify the model or group. Also to excempt some specific keys.
Note: Remember that retrieving all entries without specifying the group or model, will retrieve all entries that has no group or model set. You can consider these as (global app settings).
Settings::all(); Settings::for($model)->all(); Settings::for($model)->group('name')->all(); Settings::except('k1', 'k2')->for($model)->group('name')->all(); Settings::except('k1')->for($model)->group('name')->all();
Helper Method
The package also ships with a settings helper method, you can use it instead of using Settings Facade
settings()->set('key', 'value'); ...
HasSettings Trait
You can use HasSettings trait on your Eloquent models as well
- First prepare your model
use Ubmvn\Settings\HasSettings; class User extends Model { use HasSettings; }
- Now you can call settings() method on that model. This is equivelant to
Settings::for($model)
$user->settings()->set('k1', 'v1'); ...
Custom Repositories
If you don't want to use the database repository, you can easily create your own settings repository. To do that
- Create a class of your repository that implements Repository interface and implement the following methods
use Ubmvn\Settings\Contracts\Repository; class FileRepository implements Repository { public function get($key,$default = null){ // } public function set($key,$value = null){ // } public function forget($key){ // } public function all(){ // } }
- In settings configuration file, add your own repository config to repositories attribute
'repositories' => [ ... 'file' => [ ... ] ],
- Change the default repository in settings config file to your own repository implementation
Custom Casts
The package allows you to easily create your own casting rules of any object type.
- Create your own cast class that implements Castable interface.
Note: The set method is called when the value of the entries is being stored to the repository, and the get method is called when the value is being retrieved from the repository.
use Ubmvn\Settings\Contracts\Castable; class CustomCast implements Castable { public function set($payload){ // } public function get($payload){ // }}
- Add the casts to the array of casts in settings config file
'casts' => [
...
CustomDataType::class => CustomCast::class
]
Note: If you want to pass a parameter to your cast, you can set an object of the cast instead of cast class name
'casts' => [ ... CustomDataType::class => new CustomCast('param') ]
Settings Cache
You can easily enable or disable caching settings in settings.php config file. You can also specify which caching store to use
'cache' => [ 'enabled' => env('SETTINGS_CACHE_ENABLED', false), 'store' => null, 'prefix' => null, ],
Testing
To run the package's tests, simply call the following command
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Credits
Alternatives
License
The MIT License (MIT). Please see License File for more information.
ubmvn/laravel-settings 适用场景与选型建议
ubmvn/laravel-settings 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 274 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 09 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel-settings」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ubmvn/laravel-settings 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ubmvn/laravel-settings 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ubmvn/laravel-settings 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Persistent settings with caching in Laravel.
Notification system for laravel
Per-model preferences for Laravel with defaults and validation.
Manage settings in laravel
Settings for your Laravel App
Laravel Settings - Package for handling user settings with file based defaults and DB custom values.
统计信息
- 总下载量: 274
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-09-04