hackeresq/laravel-settings
Composer 安装命令:
composer require hackeresq/laravel-settings
包简介
Super simple key/value settings for Laravel 8+
README 文档
README
Super simple key/value settings for Laravel that natively supports encryption and multi-tenancy.
- Installation
- Usage
- Encryption
- Multi-tenancy
- Casting
- Disable cache
- Hidden settings
- Customize table name
Installation
This package can be used in Laravel 8.0+.
You can install the package via composer:
composer require hackeresq/laravel-settings
Since Laravel 5.5+, service providers and aliases will automatically get registered and you can skip this step. To use this package with older versions, please use release < 2.0.
You can publish the migration and config files, then migrate the new settings table all in one go, using:
php artisan vendor:publish --provider="HackerESQ\Settings\SettingsServiceProvider" --tag=migrations && php artisan vendor:publish --provider="HackerESQ\Settings\SettingsServiceProvider" --tag=config && php artisan migrate
Success! laravel-settings is now installed!
Usage
Settings can be accessed using the easy-to-remember Facade, Settings.
Set new setting
You can set new settings using the "set" method, which accepts an associative array of one or more key/value pairs. For security reasons, this will first check to see if such a setting key is "fillable," which is a configuration option in the config/settings.php file.
If such a key exists in the config, it will update the key to the new value passed. If the key does not exist in the fillable config, it will disregard the change. So, if this is a fresh install, do not expect the following to work:
Settings::set(['firm_name'=>'new']);
It will not set the new setting until you have either set the fillable fields in the config, or you have opted to force the setting. If you wish to force set a new setting, you should use the force() method before calling the set() method:
Settings::force()->set(['firm_name'=>'new']);
As of version 3.0.4, the global override for forcing settings has been removed from the config file for this package. Instead, you can use a wildcard for the fillable property, like this:
'fillable' => ['*']
This is more in line with standard Laravel syntax (e.g. for models).
Get all settings
If no parameters are passed to the "get" method, it will return an array of all settings:
Settings::get();
You can optionally hide specific settings using the hidden config as described below.
Get single setting
You can return a single setting by passing a single setting key:
Settings::get('firm_name');
Get certain settings
You can also return a list of specified settings by passing an array of setting keys:
Settings::get(['firm_name','contact_types']);
Check if a setting is set
Sometimes you can't know if a setting has been set or not (i.e. boolean settings that will return false if the setting does not exist, but also if the setting has been set to false).
Settings::has(['dark_theme']);
Encryption
You can define keys that should be encrypted automatically within the config/settings.php file. To do so, add the keys as such:
'encrypt' => [ 'twitter_client_id', 'twitter_client_secret', ],
Multi-tenancy
This package can be used in a multi-tenant environment. The set, get, and has methods all read an internal 'tenant' attribute that can be set with the tenant() method. You can set the 'tenant' attribute by calling the tenant() method first, like this:
Settings::tenant('tenant_name')->set(['firm_name'=>'foo bar']); // returns true (i.e. successfully set `firm name`)
Settings::tenant('tenant_name')->get('firm_name'); // returns 'foo bar'
Settings::tenant('tenant_name')->has('firm_name'); // returns true
The 'tenant' attribute passed can be any alphanumeric string. The 'tenant' attribute can also be left blank to have, for example, settings saved to a so-called "central" tenant. Note: the 'tenant' attribute is not strictly typed, and will be passed to the database query as a string.
Casting
You can cast settings to native PHP types using the cast option in the settings.php config. Here's an example of an array being cast to JSON and back to a native PHP array:
// settings.php 'cast' => [ 'array_of_values' => 'json' ]
Which allows you to do the following:
Settings::set(['array_of_values' => ['one', 'two', 'three']]); return Settings::get('array_of_values'); // returns a PHP array: ['one', 'two', 'three']
Disable cache
Depending on your use case, you may like to disable the cache (enabled by default). Disable the cache by modifying the config/settings.php file as such:
'cache' => false
Hidden settings
You may wish to hide specific settings (like API keys or other sensitive user data) from inadvertent disclosure. You can set these settings in the config/settings.php file. To do so, add the keys as such:
'hidden' => [ 'twitter_client_secret', 'super_secret_password', ],
Once these are set, they must be specifically requested using the get() method. In other words, this acts like the $hidden attribute on Laravel Eloquent models.
In addition to hiding specific settings, you can opt to hide ALL the settings (unless specifically requested, of course). To do this, you can use a wildcard:
'hidden' => ['*'],
Customize table name
For some cases, it may be necessary to customize the name of the table where settings are stored. By default, the migrations that come with this package create a 'settings' table. If, for some reason, it becomes necessary to change the default table, you can set the 'table' option in the config/settings.php file, like this:
'table' => 'user_options_table',
This configuration option is not included in the base config file as this is an edge case that is not commonly encountered; but nonetheless a nice convenience to have when it does come up.
Finally
Testing
You can run tests with the composer test command.
Contributing
Feel free to create a fork and submit a pull request if you would like to contribute.
Bug reports
Raise an issue on GitHub if you notice something broken.
hackeresq/laravel-settings 适用场景与选型建议
hackeresq/laravel-settings 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.19k 次下载、GitHub Stars 达 31, 最近一次更新时间为 2018 年 10 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「value」 「Settings」 「key」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 hackeresq/laravel-settings 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hackeresq/laravel-settings 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 hackeresq/laravel-settings 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Persistent settings package for Laravel framework.
Runn Me! Value Objects Library
A Zend Framework module to quickly and easily set PHP settings.
Settings Card for Laravel Nova.
MaxAl Subscriptions is a flexible plans and subscription management system for Laravel, with the required tools to run your SAAS like services efficiently. It's simple architecture, accompanied by powerful underlying to afford solid platform for your business.
Rinvex Subscriptions is a flexible plans and subscription management system for Laravel, with the required tools to run your SAAS like services efficiently. It's simple architecture, accompanied by powerful underlying to afford solid platform for your business.
统计信息
- 总下载量: 2.19k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 31
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-10-10