h0rseduck / yii2-settings

Yii2 设置模块

安装: 226

依赖者: 0

建议者: 0

安全性: 0

星星: 1

关注者: 2

分支: 33

类型:yii2-extension

2.2.5 2019-07-04 09:36 UTC

This package is auto-updated.

Last update: 2024-09-04 21:05:49 UTC


README

简单的 Yii2 设置扩展

Latest Stable Version Total Downloads License Build Status

安装

安装此扩展的首选方式是通过 composer.

运行以下命令

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

或者在 composer.json 的 require 部分添加

"h0rseduck/yii2-settings": "*"

配置

数据库迁移

在使用此扩展之前,我们还需要准备数据库。

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

模块设置

要访问模块,您需要在应用程序配置中配置 modules 数组

'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',
                ]
            ]
        ],
    ],
]

然后您可以通过以下 URL 访问设置页面: https:///path/to/index.php?r=admin/settings/

组件设置

要使用 Setting 组件,您需要在应用程序配置中配置 components 数组

'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();  

管理自定义设置

您可以使用自己的表单模型通过 SettingsAction 管理您 Web 应用的自定义设置。要使用 SettingsAction 类,您需要遵循以下步骤

  1. 创建自己的模型,例如
<?php

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'], 'required'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels(): array
    {
        return [
            'appName' => Yii::t('app', 'Application Name'),
            'adminEmail' => Yii::t('app', 'Admin Email'),
        ];
    }
}
  1. 创建视图文件,命名为 settings.php,并包含以下内容
<?php

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');
?>
<?php $form = ActiveForm::begin(); ?>

<?php echo $form->field($model, 'appName'); ?>

<?php echo $form->field($model, 'adminEmail'); ?>

<?php echo Html::submitButton(Yii::t('app', 'Save'), ['class' => 'btn btn-success']) ?>

<?php ActiveForm::end(); ?>
  1. 将设置操作添加到控制器类中,如下所示
<?php

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,
            ],
        ];
    }
}

现在您可以通过以下 URL 访问设置页面: https:///path/to/index.php?r=site/manage-settings/

国际化

此扩展中引入的所有文本和消息均在 'h0rseduck.settings' 类别下可翻译。您可以使用以下应用程序配置使用此扩展提供的翻译

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