PHPackages                             ed-smartass/yii2-settings - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. ed-smartass/yii2-settings

ActiveYii2-extension[Utility &amp; Helpers](/categories/utility)

ed-smartass/yii2-settings
=========================

A flexible and robust dynamic settings management extension for Yii2 applications with database storage and caching support

2.1.0(1mo ago)180MITPHPPHP &gt;=7.3CI passing

Since Aug 28Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/ed-smartass/yii2-settings)[ Packagist](https://packagist.org/packages/ed-smartass/yii2-settings)[ RSS](/packages/ed-smartass-yii2-settings/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (8)Dependencies (5)Versions (10)Used By (0)

Yii2 Settings
=============

[](#yii2-settings)

Dynamic key-value settings for Yii2 with database storage, automatic type casting, and caching.

[Русская версия](docs/README.ru.md)

[![Latest Stable Version](https://camo.githubusercontent.com/58f9ab89db8360ecb000374baca09a0daceedeed8a80dcfa35f092578d01e47d/68747470733a2f2f706f7365722e707567782e6f72672f65642d736d6172746173732f796969322d73657474696e67732f762f737461626c65)](https://packagist.org/packages/ed-smartass/yii2-settings)[![Total Downloads](https://camo.githubusercontent.com/a37693f46d4de81663c4a4b0d0297a2b632b3d612c2af7bcd4203e5f886daf88/68747470733a2f2f706f7365722e707567782e6f72672f65642d736d6172746173732f796969322d73657474696e67732f646f776e6c6f616473)](https://packagist.org/packages/ed-smartass/yii2-settings)[![License](https://camo.githubusercontent.com/5a7daaf90c41dc12447af42cbd72c8a5fa11986d6a89bbce686bb2e719d5e7d5/68747470733a2f2f706f7365722e707567782e6f72672f65642d736d6172746173732f796969322d73657474696e67732f6c6963656e7365)](https://packagist.org/packages/ed-smartass/yii2-settings)

Features
--------

[](#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 `DbDependency` auto-invalidation
- Magic property access: `$settings->key` to 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
------------

[](#installation)

```
composer require ed-smartass/yii2-settings
```

Run the migration:

```
php yii migrate --migrationPath=@vendor/ed-smartass/yii2-settings/src/migrations
```

Or register the migration path in console config:

```
return [
    'controllerMap' => [
        'migrate' => [
            'class' => 'yii\console\controllers\MigrateController',
            'migrationPath' => [
                '@console/migrations',
                '@vendor/ed-smartass/yii2-settings/src/migrations',
            ],
        ],
    ],
];
```

Configuration
-------------

[](#configuration)

```
return [
    'components' => [
        'settings' => [
            'class' => 'Smartass\Yii2Settings\Settings',

            // Table name. Default: '{{%setting}}'
            'table' => '{{%setting}}',

            // DB connection component ID. Default: 'db'
            'db' => 'db',

            // Cache component ID, or null to disable caching. Default: 'cache'
            'cache' => 'cache',

            // Cache key prefix. Default: 'settings'
            'cacheKey' => 'settings',

            // Cache TTL in seconds, null = no expiration. Default: null
            'cacheDuration' => null,

            // Replace %placeholders% in component configs. Default: false
            'processConfig' => false,
        ],
    ],
];
```

Usage
-----

[](#usage)

### Basic operations

[](#basic-operations)

```
$settings = Yii::$app->settings;

// Set
$settings->set('site.name', 'My App');
$settings->set('per_page', 25);
$settings->set('maintenance', false);
$settings->set('mail.providers', ['smtp', 'sendmail']);

// Get
$settings->get('site.name');                    // 'My App'
$settings->get('missing_key');                  // null
$settings->get('missing_key', 'fallback');      // 'fallback'
$settings->get('theme', 'default', true);       // 'default' (also saved to DB)

// Delete
$settings->delete('site.name');

// Delete all
$settings->flush();

// Force reload from database
$settings->refresh();

// Get all settings as array
$settings->settings; // ['per_page' => 25, 'maintenance' => false, ...]
```

### Magic property access

[](#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.

```
// Read / write simple keys
$name = Yii::$app->settings->site_name;
Yii::$app->settings->site_name = 'New Name';

// Dot / hyphen keys — use get()/set() or brace syntax
$host = Yii::$app->settings->get('mail.smtp-host');
Yii::$app->settings->set('mail.smtp-host', 'smtp.example.com');
$host = Yii::$app->settings->{'mail.smtp-host'};
```

### Explicit type override

[](#explicit-type-override)

By default the type is detected automatically. You can force a specific type:

```
use Smartass\Yii2Settings\Settings;

// Store numeric string as integer
$settings->set('port', '8080', Settings::TYPE_INTEGER);

// Store integer as string
$settings->set('code', 42, Settings::TYPE_STRING);
```

### Config placeholders

[](#config-placeholders)

Enable `processConfig` and add the component to `bootstrap` so the event handler is registered at application start:

```
// app config
return [
    'bootstrap' => ['settings'],
    'components' => [
        'settings' => [
            'class' => 'Smartass\Yii2Settings\Settings',
            'processConfig' => true,
        ],
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            'transport' => [
                'class' => 'Swift_SmtpTransport',
                'host' => '%mail.host|smtp.example.com%',  // setting name | default
                'port' => '%mail.port%',                    // setting name (no default)
            ],
        ],
    ],
];
```

Syntax: `%setting.name|default_value%` or `%setting.name%`

API Reference
-------------

[](#api-reference)

### `get($key, $default = null, $saveDefault = false)`

[](#getkey-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)`

[](#setkey-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)`

[](#deletekey)

Deletes a setting by key.

### `flush()`

[](#flush)

Deletes all settings.

### `refresh()`

[](#refresh)

Clears the in-memory cache and invalidates the cache entry, forcing the next read to hit the database.

### Property: `$settings`

[](#property-settings)

Returns all settings as an associative array `['key' => value, ...]`.

Supported Types
---------------

[](#supported-types)

TypeConstantPHP typeStorage`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`JSONTesting
-------

[](#testing)

```
composer install
vendor/bin/phpunit
```

License
-------

[](#license)

MIT. See [LICENSE](LICENSE).

###  Health Score

43

—

FairBetter than 91% of packages

Maintenance94

Actively maintained with recent releases

Popularity11

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 60% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~241 days

Recently: every ~95 days

Total

8

Last Release

30d ago

Major Versions

1.0.2 → 2.0.02025-03-28

### Community

Maintainers

![](https://www.gravatar.com/avatar/dfbc1cfb2ad235a55d89d24c99f2e9a772eb62974d2cfdd61f15b973aacfe67f?d=identicon)[ed-smartass](/maintainers/ed-smartass)

---

Top Contributors

[![ed-smartass](https://avatars.githubusercontent.com/u/62896221?v=4)](https://github.com/ed-smartass "ed-smartass (12 commits)")[![claude](https://avatars.githubusercontent.com/u/81847?v=4)](https://github.com/claude "claude (8 commits)")

---

Tags

configurationSettingsyii2dynamic-configdatabase settingsyii2 settings

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/ed-smartass-yii2-settings/health.svg)

```
[![Health](https://phpackages.com/badges/ed-smartass-yii2-settings/health.svg)](https://phpackages.com/packages/ed-smartass-yii2-settings)
```

###  Alternatives

[janisto/yii2-environment

Environment class for Yii 2, used to set configuration for console and web apps depending on the server environment.

2034.6k3](/packages/janisto-yii2-environment)[lav45/yii2-settings

This extension helps you to easily store and retrieve data for your application.

1615.0k2](/packages/lav45-yii2-settings)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
