PHP code example of h0rseduck / yii2-settings

1. Go to this page and download the library: Download h0rseduck/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/ */

    

h0rseduck / yii2-settings example snippets


'admin' => [
    'modules' => [
        'settings' => [
            'class' => 'h0rseduck\settings\Module',
            // Also you can override some controller properties in following way:
            'controllerMap' => [
                'default' => [
                    'class' => 'h0rseduck\settings\controllers\DefaultController',
                    'searchClass' => [
                        'class' => 'h0rseduck\settings\models\search\SettingSearch',
                        'pageSize' => 25
                    ],
                    'modelClass' => 'Your own model class',
                    'indexView' => 'custom path to index view file',
                    'createView' => 'custom path to create view file',
                    'updateView' => 'custom path to update view file',
                ]
            ]
        ],
    ],
]

'components' => [
    'settings' => [
        'class' => 'h0rseduck\settings\components\Settings',
    ],
]

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

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

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

$settings->set('section', 'key', 'false', SettingType::BOOLEAN_TYPE);

// Checking existence of setting
$settings->has('section', 'key');

// Activates a setting
$settings->activate('section', 'key');

// Deactivates a setting
$settings->deactivate('section', 'key');

// Removes a setting
$settings->remove('section', 'key');

// Removes all settings
$settings->removeAll();

$settings->invalidateCache(); // automatically called on set(), remove();  



namespace app\models\forms;

use Yii;
use yii\base\Model;

class ConfigurationForm extends Model
{
    /**
     * @var string application name
     */
    public $appName;

    /**
     * @var string admin email
     */
    public $adminEmail;

    /**
     * @inheritdoc
     */
    public function rules(): array
    {
        return [
            [['appName', 'adminEmail'], '



use yii\helpers\Html;
use yii\widgets\ActiveForm;

/* @var $model \app\models\forms\ConfigurationForm */
/* @var $this \yii\web\View */

$this->title = Yii::t('app', 'Manage Application Settings');



namespace app\controllers;

use yii\web\Controller;

/**
 * Class SiteController
 *
 * @package app\controllers
 */
class SiteController extends Controller
{
    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'manage-settings' => [
                'class' => \h0rseduck\settings\actions\SettingsAction::class,
                // also you can use events as follows:
                'on beforeSave' => function ($event) {
                    // your custom code
                },
                'on afterSave' => function ($event) {
                    // your custom code
                },
                'modelClass' => \app\models\forms\ConfigurationForm::class,
            ],
        ];
    }
}

return [
    'components' => [
        'i18n' => [
            'translations' => [
                'h0rseduck.settings' => [
                    'class' => 'yii\i18n\PhpMessageSource',
                    'basePath' => '@h0rseduck/settings/messages',
                ],
                // ...
            ],
        ],
        // ...
    ],
    // ...
];

php composer.phar 

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