ed-smartass / yii2-settings
为 Yii2 应用提供动态设置
1.0.2
2022-08-25 19:12 UTC
Requires
- yiisoft/yii2: ~2.0.14
This package is auto-updated.
Last update: 2024-09-25 23:24:11 UTC
README
安装
- 通过 composer 安装
composer require ed-smartass/yii2-settings
- 应用迁移
php yii migrate --migrationPath=@vendor/ed-smartass/yii2-settings/src/migrations
或添加到控制台配置
return [ // ... 'controllerMap' => [ // ... 'migrate' => [ 'class' => 'yii\console\controllers\MigrateController', 'migrationPath' => [ '@console/migrations', // Default migration folder '@vendor/ed-smartass/yii2-settings/src/migrations' ] ] // ... ] // ... ];
- 配置应用
return [ // ... 'bootstrap' => [ // ... 'settings' ], 'components' => [ // ... 'settings' => [ 'class' => 'Smartass\Yii2Settings\Settings', // Name of table with settings. Default: `{{%setting}}`. 'table' => '{{%setting}}', // ID of db commponent in application. Default: `db`. 'dbComponent' => 'db', // If you want to cache setting. Default: `false`. 'cache' => true, // ID of cache commponent in application. Default: `cache`. 'cacheComponent' => 'cache', // Key for storing settings in cache. Default: `settings`. 'cacheKey' => 'settings', // Cache duration for settings. Default: `null`. 'cacheDuration' => null, // If you want to change Application config set to `true` // and set value at config like `'language' => '%main.language|ru%'`. // Where: `main.language` is setting name and `ru` is default value // Default: `false`. 'processConfig' => false, ] ] // ... ];
使用方法
// List all settings Yii::$app->settings->settings // Get setting Yii::$app->settings->get('access_token_ttl'); // Or Yii::$app->settings->access_token_ttl; // Set setting Yii::$app->settings->set('access_token_ttl', 3600*24*7); // Or Yii::$app->settings->access_token_ttl = 3600*24*7; // Only if setting `access_token_ttl` already exist
方法
-
get($key, $default = null, $saveDefault = false) — 通过键获取设置
- $key — 设置的键
- $default — 如果设置不存在时的默认值
- $saveDefault — 如果设置不存在时保存默认值
-
set($key, $value, $type = null) — 设置设置
- $key — 设置的键
- $value — 设置的值(如果值为
null
,则删除设置) - $type — 设置的类型(
integer
,float
,string
,boolean
,array
),如果类型为null
,则自动检测类型
-
delete($key) — 通过键删除设置
- $key — 设置的键
-
flush() — 删除所有设置