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

ActiveYii2-extension

rafalkot/yii2-settings
======================

Simple settings management for Yii Framework 2.0

1.0.0(10y ago)12901[1 issues](https://github.com/rafalkot/yii2-settings/issues)[1 PRs](https://github.com/rafalkot/yii2-settings/pulls)MITPHP

Since Mar 2Pushed 9y ago2 watchersCompare

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

READMEChangelogDependencies (2)Versions (2)Used By (0)

Yii2-settings
=============

[](#yii2-settings)

Settings management component for Yii2 Framework.

Features
--------

[](#features)

- settings stored in DB
- simple API for read/write/remove
- categorized settings
- easy integration by SettingsTrait with your components, models, modules etc.
- SettingsForm widget

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

[](#installation)

### 1. Install via composer

[](#1-install-via-composer)

```
$ composer require rafalkot/yii2-settings

```

### 2. Add component to your app config

[](#2-add-component-to-your-app-config)

Add yii2-settings component to your configuration files

```
'components' => [
	...
	'settings' => [
		'class' => 'rafalkot\yii2settings\Settings',
		// optional configuration:
		'db' => 'db', // DB Component ID
		'preLoad' => ['category1', 'category2'] // Categories to be loaded on component initialization
	],
	...
]
```

### 3. Create DB table

[](#3-create-db-table)

```
CREATE TABLE IF NOT EXISTS `setting` (
  `category` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'system',
  `key` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
  `value` text COLLATE utf8mb4_unicode_ci NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `created_by` int(11) DEFAULT NULL,
  `updated_by` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

ALTER TABLE `setting`
 ADD PRIMARY KEY (`category`,`key`), ADD KEY `fk_setting_user1_idx` (`created_by`), ADD KEY `fk_setting_user2_idx` (`updated_by`);
```

Usage
-----

[](#usage)

### Component

[](#component)

Reading settings:

```
// will return `key` setting value from `categoryName` category or `defaultValue` (defaults sets to be null)
Yii::$app->settings->get('categoryName', 'key', 'defaultValue');

// will return an array of `key1` & `key2` settings from `categoryName` category
Yii::$app->settings->get('categoryName', ['key1', 'key2']);

// you can set default values too
Yii::$app->settings->get('categoryName', ['key1', 'key2'], ['key1' => 'key1default', 'key2' => 'key2default']);

// will return array of all settings from `categoryName` category
Yii::$app->settings->get('categoryName');
```

Saving settings:

```
// saves single setting
Yii::$app->settings->set('categoryName', 'key', 'value');

// saves multiple settings
Yii::$app->settings->set('categoryName', [
	'key1' => 'value 1',
	'key2' => 'value 2'
]);
```

Removing settings:

```
// removes single setting
Yii::$app->settings->remove('categoryName', 'key');

// removes multiple settings
Yii::$app->settings->remove('categoryName', ['key1', 'key2']);

// removes all settings from category
Yii::$app->settings->remove('categoryName');
```

Loading settings:

```
// loads settings from single category
Yii::$app->settings->load('categoryName');

// loads settings from multiple categories
Yii::$app->settings->load(['categoryName1', 'categoryName2']);
```

### SettingsTrait

[](#settingstrait)

`SettingsTrait` provides few methods to simple settings access.

Example usage (all operations are using `site` category):

```
namespace app\components;

use rafalkot\yii2settings\SettingsTrait;

class Site
{
    use SettingsTrait;

    public function getSettingsCategory()
    {
        return 'site';
    }

	public function someMethod()
	{
		$this->setSetting('key', 'value');
		$this->getSetting('key', 'defaultValue');
		$this->getSetting();
		$this->removeSetting('key');
		$this->removeSetting();
	}
}
```

### SettingsForm widget

[](#settingsform-widget)

`SettingsForm` widget renders form based on our form definition.

It could be done by overriding `getSettingsFormConfig` method from `SettingsTrait`.

Firstly, let's add form definition to our class:

```
namespace app\components;

use rafalkot\yii2settings\SettingsTrait;
use yii\jui\DatePicker;

class Site
{
    use SettingsTrait;

    public function getSettingsCategory()
    {
        return 'site';
    }

    public function getSettingsFormConfig()
    {
        return [
            // text input
            'title' => [
                'input' => 'text',
                'label' => 'Site Title'
            ],
            // dropdown list
            'comments' => [
                'input' => 'dropdown',
                'label' => 'Are comments enabled?',
                'options' => [1 => 'Yes', 0 => 'No'],
                'default' => 1
            ],
            // checkboxes
            'languages' => [
                'input' => 'checkboxList',
                'options' => ['en' => 'English', 'pl' => 'Polish']
            ],
            // custom input
            'custom_input' => [
                'input' => function ($model, $key) {
                    return DatePicker::widget(['model' => $model, 'attribute' => $key]);
                },
                'label' => 'Some label'
            ]
        ];
    }
}
```

Sample action

```
namespace app\controllers;

use yii\web\Controller;
use app\components\Site;

class Yii2SettingsController extends Controller
{
	public function actionExample()
    {
        $site = new Site();

        return $this->render('example', [
            'site' => $site
        ]);
    }
}
```

Widget's usage in view

```
use rafalkot\yii2settings\SettingsForm;

echo SettingsForm::widget([
    'object' => $site
]);
```

License
-------

[](#license)

Yii2-settings is released under the MIT license, see LICENSE file for details.

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance0

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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

Unknown

Total

1

Last Release

3724d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/22c554d41da7d9a29d2cf80d6f30dd10b6e216303aae7777d86920e7d499c2cf?d=identicon)[rafalkot](/maintainers/rafalkot)

---

Top Contributors

[![rafalkot](https://avatars.githubusercontent.com/u/5593878?v=4)](https://github.com/rafalkot "rafalkot (1 commits)")

---

Tags

Settingsconfigyii2extensioncomponent

### Embed Badge

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

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

###  Alternatives

[skeeks/cms

SkeekS CMS — control panel and tools based on php framework Yii2

13825.6k47](/packages/skeeks-cms)[nterms/yii2-mailqueue

Email queue component for yii2 that works with yii2-swiftmailer.

87129.2k2](/packages/nterms-yii2-mailqueue)[lav45/yii2-settings

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

1615.0k2](/packages/lav45-yii2-settings)[noam148/yii2-image-resize

A Yii2 component for resizing images (on the fly)

1144.6k7](/packages/noam148-yii2-image-resize)[abhi1693/yii2-config

Yii2 manage configuration from database

1311.0k1](/packages/abhi1693-yii2-config)[umanskyi31/opengraph

Created a new component for Yii2. The Open Graph component for your website

119.7k](/packages/umanskyi31-opengraph)

PHPackages © 2026

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