定制 yiier/yii2-user-setting 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

yiier/yii2-user-setting

最新稳定版本:v0.2.2

Composer 安装命令:

composer require yiier/yii2-user-setting

包简介

User Settings For Yii2

README 文档

README

User Settings For Yii2

Latest Stable Version Total Downloads Latest Unstable Version License

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist yiier/yii2-user-setting "*"

or add

"yiier/yii2-user-setting": "*"

to the require section of your composer.json file.

Configuration

Database Migrations

Before usage this extension, we'll also need to prepare the database.

php yii migrate --migrationPath=@yiier/userSetting/migrations/

Module Setup

To access the module, you need to configure the modules array in your application configuration:

'modules' => [
    'userSetting' => [
        'class' => 'yiier\userSetting\Module',
    ],
],

Component Setup

To use the Setting Component, you need to configure the components array in your application configuration:

'components' => [
    'userSetting' => [
        'class' => 'yiier\userSetting\UserSetting',
    ],
],

Usage

<?php
$setting = Yii::$app->userSetting;

$value = $setting->get('key');
$value = $setting->get('key', Yii::$app->user->id);

$setting->set('key', 125.5);
$setting->set('key', 125.5, Yii::$app->user->id);

$setting->set('key', false, Yii::$app->user->id, 'Not allowed Update Post');
$setting->set('key', false, 0, 'Not allowed Update Post');

// Checking existence of setting
$setting->has('key');
$setting->has('key', Yii::$app->user->id);

// Activates a setting
$setting->activate('key');
$setting->activate('key', Yii::$app->user->id);

// Deactivates a setting
$setting->deactivate('key');
$setting->deactivate('key', Yii::$app->user->id);

// Removes a setting
$setting->remove('key');
$setting->remove('key', Yii::$app->user->id);

// Removes all settings
$setting->removeAll();
$setting->removeAll(Yii::$app->user->id);

// Get's all values in the specific section.
$setting->getAllByUserId(Yii::$app->user->id);

$setting->invalidateCache(Yii::$app->user->id); // automatically called on set(), remove();

UserSettingAction

To use a custom settings form, you can use the included UserSettingAction.

  1. Create a model class with your validation rules.
  2. Create an associated view with an ActiveForm containing all the settings you need.
  3. Add yiier\userSetting\UserSettingAction to the controller's actions.

The settings will be stored in section taken from the form name, with the key being the field name.

Model:

<?php
class SiteForm extends Model
{

    public $siteName, $siteDescription;

    public function rules()
    {
        return [
            [['siteName', 'siteDescription'], 'string'],
        ];
    }

    public function fields()
    {
        return ['siteName', 'siteDescription'];
    }

    public function attributes()
    {
        return ['siteName', 'siteDescription'];
    }

    public function attributeLabels()
    {
        return [
            'siteName' => 'Site Name',
            'siteDescription' => 'Site Description'
        ];
    }

}

Views:

<?php $form = ActiveForm::begin(['id' => 'site-settings-form']); ?>

<?= $form->field($model, 'siteName') ?>
<?= $form->field($model, 'siteDescription') ?>
<?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>

<?php ActiveForm::end(); ?>

Controller:

public function actions() 
{
   return [
   		//....
            'site-settings' => [
                'class' => UserSettingAction::class,
                'modelClass' => 'app\models\SiteForm',
                //'scenario' => 'site',	// Change if you want to re-use the model for multiple setting form.
                //'userId' => 0', // By default use \Yii::$app->user->id
                'viewName' => 'site-settings',	// The form we need to render
                'successMessage' => '保存成功'
            ],
        //....
    ];
}

Reference

统计信息

  • 总下载量: 1.96k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 3
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-4-Clause
  • 更新时间: 2019-07-27

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固