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

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

shurik2k5/yii2-settings
=======================

Yii2 Database settings

0.9(6y ago)090BSD-3-ClausePHP

Since Nov 5Pushed 6y ago1 watchersCompare

[ Source](https://github.com/shurik2k5/yii2-settings)[ Packagist](https://packagist.org/packages/shurik2k5/yii2-settings)[ RSS](/packages/shurik2k5-yii2-settings/feed)WikiDiscussions master Synced yesterday

READMEChangelog (2)Dependencies (3)Versions (13)Used By (0)

[![Build Status](https://camo.githubusercontent.com/aeac27c66016864cbda66c4fa3796681486197709c817d53d91631800325a417/68747470733a2f2f6170692e7472617669732d63692e6f72672f73687572696b326b352f796969322d73657474696e67732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/shurik2k5/yii2-settings)[![Total Downloads](https://camo.githubusercontent.com/5d330408bc54ff9d4679367e9579ddd492ff58817bf80ef15aca240f4c899435/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f73687572696b326b352f796969322d73657474696e67732e737667)](https://packagist.org/packages/shurik2k5/yii2-settings)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/b77922339a5d4eb3c47bdd9bf08059e8e8fc65ef719d834c69a6d5a1aa34b7aa/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f73687572696b326b352f796969322d73657474696e67732f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/shurik2k5/yii2-settings/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/462ce0a721529620e4108c1b45d6b5a13b1db1072703a7b0fb60e3d1e56f0f69/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f73687572696b326b352f796969322d73657474696e67732f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/shurik2k5/yii2-settings/?branch=master)

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

[](#yii2-settings)

Is fork of Yii2 Database settings [phemellc/yii2-settings](https://github.com/phemellc/yii2-settings) with improved and experimental features.

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

[](#installation)

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

Either run

```
php composer.phar require --prefer-dist shurik2k5/yii2-settings "*"

```

or add

```
"shurik2k5/yii2-settings": "*"

```

to the require section of your `composer.json` file.

Subsequently, run

```
./yii migrate/up --migrationPath=@vendor/shurik2k5/yii2-settings/migrations
```

in order to create the settings table in your database.

Migrate from phemellc/yii2-settings
-----------------------------------

[](#migrate-from-phemellcyii2-settings)

For use this package not need change you application configuration.
This package use same namespace as phemellc/yii2-settings that you can simply replace in composer.json

```
"pheme/yii2-settings": "*"

```

to

```
"shurik2k5/yii2-settings": "*"

```

and run composer install/update

```
php composer.phar install

```

And that's all.

Usage
-----

[](#usage)

There are 2 parts to this extension. A module and a component. The module provides a simple GUI to edit your settings. The component provides a way to retrieve and save settings programmatically.

Add this to your main configuration's modules array

```
'modules' => [
    'settings' => [
        'class' => 'pheme\settings\Module',
        'sourceLanguage' => 'en'
    ],
    ...
],
```

Add this to your main configuration's components array

```
'components' => [
    'settings' => [
        'class' => 'pheme\settings\components\Settings'
    ],
    ...
]
```

Typical component usage

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

$value = $settings->get('section.key');

$value = $settings->get('key', 'section');

$value = $this->setting->getOrSet('key', 'defaultValue', 'section', 'string');

$value = $this->setting->getOrSet('section.key', 'defaultValue');

$settings->set('section.key', 'value');

$settings->set('section.key', 'value', null, 'string');

$settings->set('key', 'value', 'section', 'integer');

// Automatically called on set();
$settings->clearCache();
```

SettingsAction
--------------

[](#settingsaction)

To use a custom settings form, you can use the included `SettingsAction`.

1. Create a model class with your validation rules.
2. Create an associated view with an `ActiveForm` containing all the settings you need.
3. Add `pheme\settings\SettingsAction` to the controller's actions.

The settings will be stored in section taken from the form name, with the key being the field name.

**Model**:

```
class Site extends Model {

	public $siteName, $siteDescription;

	public function rules()
	{
		return [
			[['siteName', 'siteDescription'], 'string'],
		];
	}

	public function fields()
	{
        return ['siteName', 'siteDescription'];
	}

	public function attributes()
	{
        return ['siteName', 'siteDescription'];
	}
}
```

**Views**:

```
