outerweb/settings
Composer 安装命令:
composer require outerweb/settings
包简介
Application wide settings stored in your database
README 文档
README
Settings
This package provides application wide settings stored in your database
Table of Contents
Installation
You can install the package via composer:
composer require outerweb/settings
Run the install command to publish the migration and the config file:
php artisan settings:install
Usage
This package tries to mimic the Config functionality of Laravel as close as possible.
You can use the Outerweb\Settings\Facades\Setting facade or the setting() helper function to work with your stored settings.
Storing settings
Storing a single setting
use Outerweb\Settings\Facades\Setting; Setting::set('key', 'value'); // or setting(['key' => 'value']); // or setting()->set('key', 'value');
Storing multiple settings
use Outerweb\Settings\Facades\Setting; Setting::set([ 'key1' => 'value1', 'key2' => 'value2', ]); // or setting([ 'key1' => 'value1', 'key2' => 'value2', ]); // or setting()->set([ 'key1' => 'value1', 'key2' => 'value2', ]);
Storing nested settings
You can use "dot" notation to store nested settings:
use Outerweb\Settings\Facades\Setting; Setting::set('parent.child', 'value'); // or setting(['parent.child' => 'value']); // or setting()->set('parent.child', 'value');
This will store a single table entry with the key parent.child and the value value.
You can also store nested settings using arrays:
use Outerweb\Settings\Facades\Setting; Setting::set([ 'parent' => [ 'child1' => 'value', 'child2' => 'value', ], ]); // or setting([ 'parent' => [ 'child1' => 'value', 'child2' => 'value', ], ]); // or setting()->set([ 'parent' => [ 'child1' => 'value', 'child2' => 'value', ], ]);
This will store two table entries:
parent.child1with the valuevalueparent.child2with the valuevalue
Retrieving settings
Retrieving a single setting
use Outerweb\Settings\Facades\Setting; $value = Setting::get('key'); // or $value = setting('key'); // or $value = setting()->get('key');
This will return the value of the setting stored with the key key. If the setting does not exist, it will return null.
You can also provide a default value that will be returned if the setting does not exist:
$value = Setting::get('key', 'default_value'); // or $value = setting('key', 'default_value'); // or $value = setting()->get('key', 'default_value');
This will return default_value if the setting with the key key is null or does not exist.
Retrieving a nested setting
use Outerweb\Settings\Facades\Setting; $value = Setting::get('parent.child'); // or $value = setting('parent.child'); // or $value = setting()->get('parent.child');
This will return the value of the setting stored with the key parent.child.
Retrieving multiple settings via parent key
Imagine you have the following settings stored:
parent.child1with the valuevalue1parent.child2with the valuevalue2
use Outerweb\Settings\Facades\Setting; $values = Setting::get('parent'); // or $values = setting('parent'); // or $values = setting()->get('parent');
This will return an associative array:
[
'child1' => 'value1',
'child2' => 'value2',
]
Retrieving multiple settings
use Outerweb\Settings\Facades\Setting; $values = Setting::get(['parent.child1', 'parent.child2']); // or $values = setting(['parent.child1', 'parent.child2']); // or $values = setting()->get(['parent.child1', 'parent.child2']);
This will return an associative array:
[
'parent.child1' => 'value1',
'parent.child2' => 'value2',
]
Retrieving all settings
use Outerweb\Settings\Facades\Setting; $values = Setting::get(); // or $values = setting()->get();
This will return all settings as an associative array.
Type safety
You can use the following methods to ensure type safety when retrieving settings:
String
use Outerweb\Settings\Facades\Setting; $value = Setting::string('key'); // or $value = setting()->string('key');
This will make sure the returned value is a string. Otherwise, it will throw an UnexpectedValueException.
Integer
use Outerweb\Settings\Facades\Setting; $value = Setting::integer('key'); // or $value = setting()->integer('key');
This will make sure the returned value is an integer. Otherwise, it will throw an UnexpectedValueException.
Float
use Outerweb\Settings\Facades\Setting; $value = Setting::float('key'); // or $value = setting()->float('key');
This will make sure the returned value is a float. Otherwise, it will throw an UnexpectedValueException.
Boolean
use Outerweb\Settings\Facades\Setting; $value = Setting::boolean('key'); // or $value = setting()->boolean('key');
This will make sure the returned value is a boolean. Otherwise, it will throw an UnexpectedValueException.
Array
use Outerweb\Settings\Facades\Setting; $value = Setting::array('key'); // or $value = setting()->array('key');
This will make sure the returned value is an array. Otherwise, it will throw an UnexpectedValueException.
Collection
use Outerweb\Settings\Facades\Setting; $value = Setting::collection('key'); // or $value = setting()->collection('key');
This uses the same logic as array() but returns a Illuminate\Support\Collection instance instead of an array.
Changelog
Please see CHANGELOG for more information on what has changed recently.
License
The MIT License (MIT). Please see License File for more information.
outerweb/settings 适用场景与选型建议
outerweb/settings 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 120.72k 次下载、GitHub Stars 达 49, 最近一次更新时间为 2024 年 02 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「Settings」 「Outerweb」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 outerweb/settings 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 outerweb/settings 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 outerweb/settings 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Filament integration for the outerweb/settings package
Filament integration for spatie/laravel-translatable
Persistent settings package for Laravel framework.
Store and link files to your models
Filament integration for the outerweb/image-library package
A Zend Framework module to quickly and easily set PHP settings.
统计信息
- 总下载量: 120.72k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 49
- 点击次数: 30
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-02-11
