1. Go to this page and download the library: Download yiier/yii2-user-setting 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/ */
$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();
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'
];
}
}
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' => '保存成功'
],
//....
];
}