daycry/settings
Composer 安装命令:
composer require daycry/settings
包简介
Settings library for CodeIgniter 4
README 文档
README
CodeIgniter 4 Settings
Provides database storage and retrieval of application settings, with a fallback to the config classes.
Quick Start
- Install with Composer:
> composer require daycry/settings - Create a new migration and copy the provided class from below into it.
Settings provides a simple interface that you can use in place of calling config() to allow you to read and store
config values in the database. If the value has not been updated and saved in the database then the original value
from the config file will be used.
This allows you to save your application's default state as values in config files, all stored in version control, and still allows your users to override those settings once the site is live.
Installation
Install easily via Composer to take advantage of CodeIgniter 4's autoloading capabilities and always be up-to-date:
> composer require daycry/settings
Manual installation
Download this repo and then enable it by editing app/Config/Autoload.php and adding the Daycry\Settings namespace to the $psr4 array. For example, if you copied it into app/ThirdParty:
$psr4 = [ 'Config' => APPPATH . 'Config', APP_NAMESPACE => APPPATH, 'App' => APPPATH, 'Daycry\Settings' => APPPATH .'ThirdParty/settings/src', ];
Setup
Run command:
> php spark settings:publish
This command will copy a config file to your app namespace.
Then you can adjust it to your needs. By default file will be present in app/Config/Settings.php.
In order to store the settings in the database, you can run the provided migration:
> php spark migrate --all
This will also migrate all other packages. If you don't want to do that you can copy the file
from vendor/daycry/settings/src/Database/Migrations/2021-09-01-000001_CreateSettingsTable.php
into app/Database/Migrations, and migrate without the --all flag.
dot Notation
This library uses what we call "dot notation" to specify the class name and the property name to use. These are joined by a dot, hence the name.
If you have a class named App, and the property you are wanting to use is siteName, then the key
would be App.siteName.
Usage
To retrieve a config value use the settings service.
// The same as config('App')->siteName; $siteName = service('settings')->get('App.siteName');
In this case we used the short class name, App, which the config() method automatically locates within the
app/Config directory. If it was from a module, it would be found there. Either way, the fully qualified name
is automatically detected by the Settings class to keep values separated from config files that may share the
same name but different namespaces. If no config file match is found, the short name will be used, so it can
be used to store settings without config files.
To save a value, call the set() method on the settings class, providing the class name, the key, and the value.
Note that boolean true/false will be converted to strings :true and :false when stored in the database, but
will be converted back into a boolean when retrieved. Arrays and objects are serialized when saved, and unserialized
when retrieved.
service('settings')->set('App.siteName', 'My Great Site');
You can delete a value from the persistent storage with the forget() method. Since it is removed from the storage,
it effectively resets itself back to the default value in config file, if any.
service('settings')->forget('App.siteName')
Using the Helper
The helper provides a shortcut to the using the service. It must first be loaded using the helper() method
or telling your BaseController to always load it.
helper('setting'); $name = setting('App.siteName'); // Store a value setting('App.siteName', 'My Great Site'); // Using the service through the helper $name = setting()->get('App.siteName'); setting()->set('App.siteName', 'My Great Site'); // Forgetting a value setting()->forget('App.siteName');
Known Limitations
The following are known limitations of the library:
- Using the
setting()helper method does not support setting anullvalue on a property. For most cases, you are better off forgetting the value or setting it to an empty string. If you need to, you can set it by grabbing the service and using theset()method:
service('settings')->set('App.siteName', null); setting()->set('App.siteName', null);
- You can currently only store a single setting at a time. While the
DatabaseHandleruses a local cache to keep performance as high as possible for reads, writes must be done one at a time. - You can only access the first level within a property directly. In most config classes this is a non-issue,
since the properties are simple values. Some config files, like the
databasefile, contain properties that are arrays.
daycry/settings 适用场景与选型建议
daycry/settings 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.33k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2021 年 09 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「config」 「codeigniter」 「Settings」 「codeigniter4」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 daycry/settings 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 daycry/settings 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 daycry/settings 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Store your language lines in the database, yaml or other sources
A Zend Framework module to quickly and easily set PHP settings.
The CodeIgniter Redis package
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
A PSR-7 compatible library for making CRUD API endpoints
统计信息
- 总下载量: 4.33k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 6
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-09-01