dev-master
9999999-devDB settings for Yii2
MIT
The Requires
settings yii2
DB settings for Yii2
DB settings for Yii2., (*1)
The preferred way to install this extension is through composer., (*2)
Either run, (*3)
php composer.phar require --prefer-dist platx/yii2-settings "*"
or add, (*4)
"platx/yii2-settings": "*"
to the require section of your composer.json
file., (*5)
First, you have to create migration, that creates table for settings., (*6)
class m000000_000004_create_table_setting extends \platx\settings\CreateSettingTableMigration{}
For insert new settings, create migration, that extends from \platx\settings\SettingsMigration and implements parameter protected $_rows, like this:, (*7)
protected $_rows = [ [ self::FIELD_SECTION => 'general', self::FIELD_KEY => 'string_setting', self::FIELD_NAME => 'String setting', self::FIELD_HINT => 'Text setting with max value = 255', self::FIELD_VALUE => 'Here some default value of setting', self::FIELD_TYPE => Setting::TYPE_TEXT, self::FIELD_RULES => [ ['string', 'max' => 255], ] ], [ self::FIELD_SECTION => 'general', self::FIELD_KEY => 'integer_setting', self::FIELD_NAME => 'Integer setting', self::FIELD_HINT => 'Setting with integer value', self::FIELD_VALUE => '235', self::FIELD_TYPE => Setting::TYPE_TEXT, self::FIELD_RULES => [ ['integer', 'max' => 500], ] ], [ self::FIELD_SECTION => 'general', self::FIELD_KEY => 'text_setting', self::FIELD_NAME => 'Text setting', self::FIELD_HINT => 'Setting with big text value', self::FIELD_VALUE => 'some big text', self::FIELD_TYPE => Setting::TYPE_TEXTAREA, self::FIELD_RULES => [ ['string'], ] ], [ self::FIELD_SECTION => 'general', self::FIELD_KEY => 'checkbox_setting', self::FIELD_NAME => 'Checkbox setting', self::FIELD_HINT => 'Checkbox setting with true or false condition', self::FIELD_VALUE => '0', self::FIELD_TYPE => Setting::TYPE_CHECKBOX, self::FIELD_RULES => [ ['integer'], ] ], ];
For use setting value in your code, try:, (*8)
$settingValue = \platx\settings\Setting::get('section.key');
Where section
- Setting section, key
- setting key., (*9)
For update setting value, use following code:, (*10)
\platx\settings\Setting::set('section.key', 'Here put new value for setting');
For update settings for all section, use \platx\settings\SettingForm
class. Also you can use in your admin controller action
platx\settings\SettingAction
and in your view file you can do something like this:, (*11)
<?php $form = ActiveForm::begin([ 'options' => ['class' => 'form-horizontal'], 'fieldConfig' => [ 'template' => '{label}<div class="col-sm-10">{input}{error}{hint}</div>', 'labelOptions' => ['class' => 'col-sm-2 control-label'], ], ]); ?> <?php foreach ($settingForm->getSettings() as $key => $settingModel) : ?> <?php if ($settingModel->type_key == $settingModel::TYPE_TEXT) { $field = $form->field($settingForm, $key); } if ($settingModel->type_key == $settingModel::TYPE_TEXTAREA) { $field = $form->field($settingForm, $key)->textarea(['rows' => 8]); } if ($settingModel->type_key == $settingModel::TYPE_EDITOR) { $field = $form->field($settingForm, $key)->widget(CKEditor::className(), [ 'options' => ['rows' => 6], 'preset' => 'full' ]); } if ($settingModel->type_key == $settingModel::TYPE_SELECTBOX) { $field = $form->field($settingForm, $key)->dropDownList($settingModel->getVariants()); } if ($settingModel->type_key == $settingModel::TYPE_SELECTBOX_MULTIPLE) { $field = $form->field($settingForm, $key)->dropDownList($settingModel->getVariants(), ['multiple' => true]); } if ($settingModel->type_key == $settingModel::TYPE_CHECKBOX) { $field = $form->field($settingForm, $key)->checkbox(); } if ($settingModel->type_key == $settingModel::TYPE_RADIO) { $field = $form->field($settingForm, $key)->radio(); } if ($settingModel->type_key == $settingModel::TYPE_RADIOLIST) { $field = $form->field($settingForm, $key)->radioList($settingModel->getVariants()); } echo $field->hint($settingModel->hint); ?> <?php endforeach; ?> <?= Html::submitButton('Save', ['class' => 'btn btn-primary']) ?> <?php $form->end(); ?>
DB settings for Yii2
MIT
settings yii2