1. Go to this page and download the library: Download rafalkot/yii2-settings library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
rafalkot / yii2-settings example snippets
'components' => [
...
'settings' => [
'class' => 'rafalkot\yii2settings\Settings',
// optional configuration:
'db' => 'db', // DB Component ID
'preLoad' => ['category1', 'category2'] // Categories to be loaded on component initialization
],
...
]
// 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');
// 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');
// loads settings from single category
Yii::$app->settings->load('categoryName');
// loads settings from multiple categories
Yii::$app->settings->load(['categoryName1', 'categoryName2']);
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();
}
}
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'
]
];
}
}
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
]);
}
}
use rafalkot\yii2settings\SettingsForm;
echo SettingsForm::widget([
'object' => $site
]);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.