yii2mod / yii2-settings
Yii2 设置模块
2.5
2018-08-20 19:39 UTC
Requires
- php: >=7.1.0
- yii2mod/yii2-editable: *
- yii2mod/yii2-enum: *
- yiisoft/yii2: ~2.0.10
Requires (Dev)
- friendsofphp/php-cs-fixer: ~2.0
- phpunit/phpunit: ^7.3
README
Yii2 设置扩展
为 Yii2 提供持久、全局的设置。
支持我们
您的业务是否依赖于我们的贡献?通过以下方式联系我们并支持我们:Patreon。所有承诺都将用于维护和新功能的人力分配。
安装
安装此扩展的首选方式是通过 composer。
或者运行
php composer.phar require --prefer-dist yii2mod/yii2-settings "*"
或者在您的 composer.json 文件的 require 部分添加以下内容
"yii2mod/yii2-settings": "*"
配置
数据库迁移
在开始使用此扩展之前,我们还需要准备数据库。
php yii migrate --migrationPath=@vendor/yii2mod/yii2-settings/migrations
模块设置
要访问模块,您需要配置应用程序配置中的 modules 数组
'modules' => [ 'settings' => [ 'class' => 'yii2mod\settings\Module', ], ],
然后您可以通过以下 URL 访问设置管理部分
http://localhost/path/to/index.php?r=settings
或者如果您已启用漂亮的 URL,您可以使用以下 URL
http://localhost/path/to/index.php/settings
组件设置
要使用 Setting 组件,您需要配置应用程序配置中的 components 数组
'components' => [ 'settings' => [ 'class' => 'yii2mod\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(); // Get's all values in the specific section. $settings->getAllBySection('section'); $settings->invalidateCache(); // automatically called on set(), remove();
管理自定义设置
您可以使用自己的表单模型通过 SettingsAction
管理您的 Web 应用程序的自定义设置。要使用 SettingsAction
类,您需要遵循以下步骤
- 创建自己的模型,例如
<?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'), ]; } }
- 创建名为
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(); ?>
- 将设置操作添加到您的控制器类中,如下所示
<?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' => \yii2mod\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 访问设置页面: http://localhost/path/to/index.php?r=site/manage-settings/
国际化
本扩展中引入的所有文本和消息均可在 'yii2mod.settings' 类别下进行翻译。您可以使用以下应用程序配置使用本扩展提供的翻译
return [ 'components' => [ 'i18n' => [ 'translations' => [ 'yii2mod.settings' => [ 'class' => 'yii\i18n\PhpMessageSource', 'basePath' => '@yii2mod/settings/messages', ], // ... ], ], // ... ], // ... ];