PHPackages                             lav45/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. lav45/yii2-settings

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

lav45/yii2-settings
===================

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

1.3.1(3y ago)1615.0k↓25.3%42BSD-3-ClausePHPPHP &gt;=5.5.0CI passing

Since Sep 20Pushed 1y ago6 watchersCompare

[ Source](https://github.com/lav45/yii2-settings)[ Packagist](https://packagist.org/packages/lav45/yii2-settings)[ Docs](https://github.com/lav45/yii2-settings)[ RSS](/packages/lav45-yii2-settings/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (3)Versions (17)Used By (2)

yii2-settings
=============

[](#yii2-settings)

[![Latest Stable Version](https://camo.githubusercontent.com/5a6e23979ad159bd0783012beaefd7ca8b3b0ec02a7e1073baefce15b50601d0/68747470733a2f2f706f7365722e707567782e6f72672f6c617634352f796969322d73657474696e67732f762f737461626c653f63616368653d636c656172)](https://packagist.org/packages/lav45/yii2-settings)[![License](https://camo.githubusercontent.com/6ec33287cae5eaf478a5228da89dade02dcf8652c5be2d54657fddf7dfac4eb6/68747470733a2f2f706f7365722e707567782e6f72672f6c617634352f796969322d73657474696e67732f6c6963656e7365)](https://packagist.org/packages/lav45/yii2-settings)[![Total Downloads](https://camo.githubusercontent.com/7d0d5788e2a18684d97add7a74b07117470b0b05c8d9886a7f1841f7df3519b2/68747470733a2f2f706f7365722e707567782e6f72672f6c617634352f796969322d73657474696e67732f646f776e6c6f616473)](https://packagist.org/packages/lav45/yii2-settings)[![Test Status](https://github.com/lav45/yii2-settings/workflows/test/badge.svg)](https://github.com/lav45/yii2-settings/actions)[![Code Coverage](https://camo.githubusercontent.com/396175941a1b8d9182437b27acdfffc274ea10425971553cea8cd9c517f457fc/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6c617634352f796969322d73657474696e67732f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/lav45/yii2-settings/)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/72994d5d764401dbd484e3d3dd365825fead1183a9e6858292c72db9514095bd/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6c617634352f796969322d73657474696e67732f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/lav45/yii2-settings/)

This extension is very useful for storing any settings, for your application.

Installation
------------

[](#installation)

The preferred way to install this extension through [composer](http://getcomposer.org/download/).

You can set the console

```
~$ composer require lav45/yii2-settings
```

in `require` section in `composer.json` file.

### Migrate

[](#migrate)

Apply with the console command:

```
~$ yii migrate/up --migrationPath=vendor/lav45/yii2-settings/migrations

```

or add it to your console config file ( console/config/main.php )

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

Component Setup
---------------

[](#component-setup)

To use the Setting Component, you need to configure the components array in your application configuration:

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

            // You can add an arbitrary prefix to all keys
            // 'keyPrefix' => 'key_',

            // DbStorage use as default storage
            // 'storage' => [
            //    'class' => 'lav45\settings\storage\DbStorage',
            //    'tableName' => '{{%settings}}',
            //    'db' => 'db',
            // ],

            'as cache' => [
                'class' => 'lav45\settings\behaviors\CacheBehavior',
                // 'cache' => 'cache',
                // 'cacheDuration' => 3600,
            ],
            'as access' => [
                'class' => 'lav45\settings\behaviors\QuickAccessBehavior',
            ],
            'as context' => [
                'class' => 'lav45\settings\behaviors\ContextBehavior',
            ],
        ],

        /**
         * FileStorage this is the adapter allows you to store your settings in a simple file
         */
        'configFile' => [
            'class' => 'lav45\settings\Settings',

            // Be used for data serialization
            'serializer' => ['serialize', 'unserialize'],

            'storage' => [
                'class' => 'lav45\settings\storage\FileStorage',
                // it is desirable to determine the storage location
                // of your configuration files in a convenient place
                // 'path' => '@runtime/settings',
                // 'dirMode' => 0755,
                // 'fileSuffix' => '.bin',
            ],
        ],

        /**
         * PhpFileStorage this is an adapter to store data in php file
         * the serializer can be disabled to increase performance
         */
        'configPhpFile' => [
            'class' => 'lav45\settings\Settings',
            'serializer' => false,
            'storage' => [
                'class' => 'lav45\settings\storage\PhpFileStorage',
                // 'path' => '@runtime/settings',
                // 'fileSuffix' => '.php',
            ],
        ],
    ]
];
```

Using
-----

[](#using)

### Can default

[](#can-default)

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

// Get not exist key
$settings->get('key'); // => null

// Get default value if key exist
$settings->get('key', []); // => []

// Get default value from the callback function
$settings->get('enable', function () {
    return true;
}); // => true

// Save and get data
$settings->set('array', ['data']); // => true
$settings->get('array'); // => [0 => 'data']

$settings->set('object', new User()); // => true
$settings->get('object'); // => User

$settings->set('float', 123.5); // => true
$settings->get('float'); // => 123.5

$settings->set('integer', 0); // => true
$settings->get('integer'); // => 0

$settings->set('bool', false); // => true
$settings->get('bool'); // => false

$settings->set('string', 'text'); // => true
$settings->get('string'); // => text

$settings->set('null', null); // => true
$settings->get('null'); // => null

// delete data by key
$settings->delete('array'); // => true

// Use as array
$settings['array'] = ['data'];

print_r($settings['array']); // => [0 => 'data']

isset($settings['array']) // => true

unset($settings['array']);
```

### CacheBehavior

[](#cachebehavior)

The extension, which will help to speed up data loading by caching. If the data changes, the cache will be updated automatically.

To clean the cache, you can use this method

```
Yii::$app->settings->cache->flush(); // => true
```

### QuickAccessBehavior

[](#quickaccessbehavior)

This extension allows you to quickly obtain the necessary data from a multidimensional array

```
// Getting data from multidimensional array
$data = [
    'options' => [
        'css' => ['bootstrap.css'],
        'js' => ['jquery', 'bootstrap.js']
    ]
];
$settings = Yii::$app->settings;

// Save data
$settings->set('array', $data); // => true

// Get data by key
$settings->get('array.options.js'); // => ['jquery', 'bootstrap.js']

// Use as array
print_r($settings['array.options.js']); // => ['jquery', 'bootstrap.js']
print_r($settings['array']['options']['js']); // => ['jquery', 'bootstrap.js']

// Get not exist key
$settings->get('array.options.img'); // => null

// Get default value if key exist
$settings->get('array.options.imgs', []); // => []

// Replace value by path key
$settings->replace('array', 'options.js', ['jquery']);
$settings->replace('array', 'options.img', ['icon.jpg', 'icon.png']);
$settings->get('array.options.js'); // => ['jquery']
$settings->get('array');
/**
 * [
 *     'options' => [
 *         'css' => ['bootstrap.css'],
 *         'js' => ['jquery'],                  // rewrite
 *         'img' => ['icon.jpg', 'icon.png'],   // added
 *     ]
 * ]
 */
```

### ContextBehavior

[](#contextbehavior)

This extension allows to retrieve data depending on the context. For example, depending on the selected language.

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

$settings->context('en-US')->set('key', ['data']); // => true

$settings->context('en-US')->get('key'); // => ['data']

$settings->context('ru-RU')->get('key'); // => null
```

### Practical use: you can see in

[](#practical-use-you-can-see-in)

- [SettingsForm](examples/SettingsForm)
- [ProjectConfiguration](https://github.com/LAV45/yii2-project-configuration)

License
-------

[](#license)

**yii2-settings** it is available under a BSD 3-Clause License. Detailed information can be found in the `LICENSE.md`.

###  Health Score

42

—

FairBetter than 90% of packages

Maintenance36

Infrequent updates — may be unmaintained

Popularity36

Limited adoption so far

Community18

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 97.4% 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 ~209 days

Recently: every ~463 days

Total

16

Last Release

386d ago

PHP version history (2 changes)1.2.1PHP &gt;=5.5.0

1.4.x-devPHP &gt;=7.3.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/675367?v=4)[Alex](/maintainers/LAV45)[@lav45](https://github.com/lav45)

---

Top Contributors

[![lav45](https://avatars.githubusercontent.com/u/675367?v=4)](https://github.com/lav45 "lav45 (74 commits)")[![nohnaimer](https://avatars.githubusercontent.com/u/8865830?v=4)](https://github.com/nohnaimer "nohnaimer (2 commits)")

---

Tags

Settingsconfigyii2extensionquick access

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[abhi1693/yii2-config

Yii2 manage configuration from database

1311.0k1](/packages/abhi1693-yii2-config)[dmstr/yii2-cookie-consent

Yii2 Cookie Consent Widget

1452.6k](/packages/dmstr-yii2-cookie-consent)[richardfan1126/yii2-js-register

Yii2 widget to register JS into view

1357.2k7](/packages/richardfan1126-yii2-js-register)

PHPackages © 2026

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