Download the PHP package ed-smartass/yii2-settings without Composer
On this page you can find all versions of the php package ed-smartass/yii2-settings. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ed-smartass/yii2-settings
More information about ed-smartass/yii2-settings
Files in ed-smartass/yii2-settings
Package yii2-settings
Short Description A flexible and robust dynamic settings management extension for Yii2 applications with database storage and caching support
License MIT
Informations about the package yii2-settings
Yii2 Settings
Dynamic key-value settings for Yii2 with database storage, automatic type casting, and caching.
Русская версия
Features
- Database-backed key-value storage
- Five value types:
integer,float,string,boolean,array(JSON) - Automatic type detection on write, strict type casting on read
- Optional caching with
DbDependencyauto-invalidation - Magic property access:
$settings->keyto read/write - Config placeholders: inject settings into component configs via
%setting.name|default% - Works with any RDBMS supported by Yii2 (MySQL, PostgreSQL, SQLite, etc.)
- PHP 7.3+ and PHP 8.x compatible
Installation
Run the migration:
Or register the migration path in console config:
Configuration
Usage
Basic operations
Magic property access
Magic properties work for keys that are valid PHP identifiers (e.g. site_name).
Keys containing dots or hyphens (e.g. site.name, smtp-host) require get()/set() or brace syntax.
Explicit type override
By default the type is detected automatically. You can force a specific type:
Config placeholders
Enable processConfig and add the component to bootstrap so the event handler is registered at application start:
Syntax: %setting.name|default_value% or %setting.name%
API Reference
get($key, $default = null, $saveDefault = false)
Returns the value for $key, or $default if not found. When $saveDefault is true, the default value is persisted to the database.
set($key, $value, $type = null)
Creates or updates a setting. Passing null as $value deletes the setting. The $type parameter accepts one of the Settings::TYPE_* constants; when omitted, the type is detected automatically.
delete($key)
Deletes a setting by key.
flush()
Deletes all settings.
refresh()
Clears the in-memory cache and invalidates the cache entry, forcing the next read to hit the database.
Property: $settings
Returns all settings as an associative array ['key' => value, ...].
Supported Types
| Type | Constant | PHP type | Storage |
|---|---|---|---|
integer |
Settings::TYPE_INTEGER |
int |
String representation |
float |
Settings::TYPE_FLOAT |
float |
String representation |
string |
Settings::TYPE_STRING |
string |
As-is |
boolean |
Settings::TYPE_BOOLEAN |
bool |
0 / 1 |
array |
Settings::TYPE_ARRAY |
array |
JSON |
Testing
License
MIT. See LICENSE.