2017 © Pedro Peláez
 

yii2-extension yii2-settings

Yii2 Settings Component

image

justcoded/yii2-settings

Yii2 Settings Component

  • Thursday, November 2, 2017
  • by justcoded
  • Repository
  • 3 Watchers
  • 1 Stars
  • 194 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 2 Versions
  • 21 % Grown

The README.md

, (*1)

Yii2 Settings Extension


Replacement for Yii app params. Easy to use component to store application settings. Supports only DB storage for now. Have ready to use base Settings form model and controller Action., (*2)

Installation

The preferred way to install this extension is through composer., (*3)

Either run, (*4)

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

or add, (*5)

"justcoded/yii2-settings": "*"

to the require section of your composer.json., (*6)

Configuration

Database migration

Before usage this extension, we'll also need to prepare the database., (*7)

You can add migrations path to your console config and then run migrate command:, (*8)

'migrate' => [
    'migrationPath' => [
        '@vendor/justcoded/yii2-settings/migrations'
    ],
],

or you can run the command below:, (*9)

php yii migrate --migrationPath=@vendor/justcoded/yii2-settings/migrations

Component Setup

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

'components' => [
    'settings' => [
        'class'     => 'justcoded\yii2\settings\components\DbSettings',
    ],
],

and add component name to bootstrap array, (*11)

    'bootstrap'  => ['log', 'settings'],

Usage

// set value
Yii::$app->settings->set('section_name', 'key', 'value');

// get value
$value = Yii::$app->settings->get('section_name', 'key');

There is a possibility to use models as some setting group object. To do this you have to add modelsMap array to component's configuration:, (*12)

    'settings' => [
        'class'     => 'justcoded\yii2\settings\components\DbSettings',
        'modelsMap' => [
            'section1' => 'app\models\MySettingsForm1',
            'section2' => 'app\models\MySettingsForm2',
        ],
    ],

Add action to controller to get settings form with keys according to the model's properties, (*13)

    public function actions()
    {
        return [
            'actionName' => [
                'class' => 'justcoded\yii2\settings\actions\SettingsAction',
                'modelClass' => 'app\models\MySettingsForm1',
            ],
        ];
    }

and create view with some active form. (You can copy a template from extension "views" folder), (*14)

Now you can get settings in better way:, (*15)

$value = Yii::$app->settings->section1->myPropertyName;

This is very useful, if you overwrite Yii/Application classes and specify correct PHPDoc comments. In this way IDE will highlight all sections and properties., (*16)

Example

You can check the example on our Yii2 starter kit., (*17)

The Versions

02/11 2017

dev-master

9999999-dev

Yii2 Settings Component

  Sources   Download

BSD-3-Clause

The Requires

 

settings config yii2 storage

22/09 2017

1.0

1.0.0.0

Yii2 Settings Component

  Sources   Download

BSD-3-Clause

The Requires

 

settings config yii2 storage