├── AutoloadExample.php ├── Bootstrap.php ├── Module.php ├── README.md ├── Setting.php ├── composer.json ├── controllers └── DefaultController.php ├── messages ├── en │ └── setting.php └── zh-CN │ └── setting.php ├── migrations └── m141208_201488_setting_init.php ├── models └── Setting.php ├── views └── default │ └── index.php └── yii2-setting-preview.png /AutoloadExample.php: -------------------------------------------------------------------------------- 1 | getUrlManager()->addRules( 19 | [ 20 | 'POST <_m:blogs>' => '<_m>/user/create', 21 | '<_m:blogs>' => '<_m>/default/index', 22 | '<_m:blogs>/-' => '<_m>/default/view', 23 | ] 24 | )*/; 25 | 26 | // Add module I18N category. 27 | if (!isset($app->i18n->translations['funson86/setting']) && !isset($app->i18n->translations['funson86/*'])) { 28 | $app->i18n->translations['funson86/setting'] = [ 29 | 'class' => 'yii\i18n\PhpMessageSource', 30 | 'basePath' => '@funson86/setting/messages', 31 | 'forceTranslation' => true, 32 | 'fileMap' => [ 33 | 'funson86/setting' => 'setting.php', 34 | ] 35 | ]; 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Module.php: -------------------------------------------------------------------------------- 1 | setViewPath('@funson86/setting/views'); 17 | } 18 | 19 | /** 20 | * Translates a message to the specified language. 21 | * 22 | * This is a shortcut method of [[\yii\i18n\I18N::translate()]]. 23 | * 24 | * The translation will be conducted according to the message category and the target language will be used. 25 | * 26 | * You can add parameters to a translation message that will be substituted with the corresponding value after 27 | * translation. The format for this is to use curly brackets around the parameter name as you can see in the following example: 28 | * 29 | * ```php 30 | * $username = 'Alexander'; 31 | * echo \Yii::t('app', 'Hello, {username}!', ['username' => $username]); 32 | * ``` 33 | * 34 | * Further formatting of message parameters is supported using the [PHP intl extensions](http://www.php.net/manual/en/intro.intl.php) 35 | * message formatter. See [[\yii\i18n\I18N::translate()]] for more details. 36 | * 37 | * @param string $category the message category. 38 | * @param string $message the message to be translated. 39 | * @param array $params the parameters that will be used to replace the corresponding placeholders in the message. 40 | * @param string $language the language code (e.g. `en-US`, `en`). If this is null, the current 41 | * [[\yii\base\Application::language|application language]] will be used. 42 | * 43 | * @return string the translated message. 44 | */ 45 | public static function t($category, $message, $params = [], $language = null) 46 | { 47 | return Yii::t('funson86/' . $category, $message, $params, $language); 48 | } 49 | 50 | /** 51 | * Check if module is used for backend application. 52 | * 53 | * @return boolean true if it's used for backend application 54 | */ 55 | public function getIsBackend() 56 | { 57 | if ($this->_isBackend === null) { 58 | $this->_isBackend = strpos($this->controllerNamespace, 'backend') === false ? false : true; 59 | } 60 | 61 | return $this->_isBackend; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Yii2 Setting 2 | ========= 3 | Yii2 Setting for other application, especially for [Yii2 Adminlte](https://github.com/funson86/yii2-adminlte) 4 | 5 | Installation 6 | ------------ 7 | 8 | The preferred way to install this extension is through [composer](http://getcomposer.org/download/). 9 | 10 | Either run 11 | 12 | ``` 13 | php composer.phar require funson86/yii2-setting "*" 14 | ``` 15 | 16 | or add 17 | 18 | ``` 19 | "funson86/yii2-setting": "*" 20 | ``` 21 | 22 | to the require section of your `composer.json` file. 23 | 24 | 25 | Usage 26 | ----- 27 | 28 | Once the extension is installed, simply use it in your code by : 29 | 30 | ### Migration 31 | 32 | Migration run 33 | 34 | ```php 35 | yii migrate --migrationPath=@funson86/setting/migrations 36 | ``` 37 | 38 | ### Config /common/config/main.php to use Yii::$app->setting 39 | ```php 40 | 'components' => [ 41 | 'setting' => [ 42 | 'class' => 'funson86\setting\Setting', 43 | ], 44 | ], 45 | ``` 46 | 47 | ### Config backend modules in backend/config/main.php to manage settings 48 | 49 | ```php 50 | 'modules' => [ 51 | 'setting' => [ 52 | 'class' => 'funson86\setting\Module', 53 | 'controllerNamespace' => 'funson86\setting\controllers' 54 | ], 55 | ], 56 | ``` 57 | 58 | 59 | ### Config at backend 60 | backend : http://you-domain/backend/web/setting 61 | 62 | ### Add Your Setting 63 | Setting support 3 type of setting: text, password, select. 64 | You could add your setting by migration or insert to table `setting` manually. 65 | ```php 66 | INSERT INTO `setting` (`id`, `parent_id`, `code`, `type`, `store_range`, `store_dir`, `value`, `sort_order`) VALUES 67 | (11, 0, 'info', 'group', '', '', '', '50'), 68 | (21, 0, 'basic', 'group', '', '', '', '50'), 69 | (31, 0, 'smtp', 'group', '', '', '', '50'), 70 | (1111, 11, 'siteName', 'text', '', '', 'Your Site', '50'), 71 | (1112, 11, 'siteTitle', 'text', '', '', 'Your Site Title', '50'), 72 | (1113, 11, 'siteKeyword', 'text', '', '', 'Your Site Keyword', '50'), 73 | (2111, 21, 'timezone', 'select', '-12,-11,-10,-9,-8,-7,-6,-5,-4,-3.5,-3,-2,-1,0,1,2,3,3.5,4,4.5,5,5.5,5.75,6,6.5,7,8,9,9.5,10,11,12', '', '8', '50'), 74 | (2112, 21, 'commentCheck', 'select', '0,1', '', '1', '50'), 75 | (3111, 31, 'smtpHost', 'text', '', '', 'localhost', '50'), 76 | (3112, 31, 'smtpPort', 'text', '', '', '', '50'), 77 | (3113, 31, 'smtpUser', 'text', '', '', '', '50'), 78 | (3114, 31, 'smtpPassword', 'password', '', '', '', '50'), 79 | (3115, 31, 'smtpMail', 'text', '', '', '', '50'); 80 | ``` 81 | 82 | ### Use Your Setting 83 | Once you set the value at the backend. Simply access your setting by the following code: 84 | 85 | ```php 86 | echo Yii::$app->setting->get('siteName'); 87 | ``` 88 | 89 | Preview: 90 | ------- 91 | ![Yii2-Setting](yii2-setting-preview.png) 92 | -------------------------------------------------------------------------------- /Setting.php: -------------------------------------------------------------------------------- 1 | where(['code' => $code])->one(); 13 | 14 | if($setting) 15 | return $setting->value; 16 | else 17 | return ; 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "funson86/yii2-setting", 3 | "description": "Yii2 Setting for other application", 4 | "type": "yii2-extension", 5 | "keywords": ["yii2","extension","setting"], 6 | "license": "Apache-2.0", 7 | "authors": [ 8 | { 9 | "name": "Funson Lee", 10 | "email": "funson86@gmail.com" 11 | } 12 | ], 13 | "require": { 14 | "yiisoft/yii2": "*" 15 | }, 16 | "autoload": { 17 | "psr-4": { 18 | "funson86\\setting\\": "" 19 | } 20 | }, 21 | "extra": { 22 | "bootstrap": "funson86\\setting\\Bootstrap" 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /controllers/DefaultController.php: -------------------------------------------------------------------------------- 1 | [ 17 | 'class' => VerbFilter::className(), 18 | 'actions' => [ 19 | 'delete' => ['post'], 20 | ], 21 | ], 22 | 'access' => [ 23 | 'class' => AccessControl::className(), 24 | 'rules' => [ 25 | [ 26 | 'allow' => true, 27 | 'roles' => ['@'] 28 | ] 29 | ] 30 | ], 31 | ]; 32 | } 33 | 34 | public function actionIndex() 35 | { 36 | //if(!Yii::$app->user->can('readPost')) throw new HttpException(403, 'No Auth'); 37 | 38 | if(Yii::$app->request->isPost) 39 | { 40 | $setting = Yii::$app->request->post('Setting'); 41 | foreach($setting as $key => $value) { 42 | Setting::updateAll(['value' => $value], ['code' => $key]); 43 | } 44 | } 45 | 46 | $settingParent = Setting::find()->where(['parent_id' => 0])->orderBy(['sort_order' => SORT_ASC])->all(); 47 | return $this->render('index', [ 48 | 'settingParent' => $settingParent, 49 | ]); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /messages/en/setting.php: -------------------------------------------------------------------------------- 1 | 'Create ', 5 | 'Create' => 'Create', 6 | 'Update ' => 'Update ', 7 | 'Update' => 'Update', 8 | 'Delete' => 'Delete', 9 | 10 | 'Setting' => 'Setting', 11 | 12 | 'info' => 'Info', 13 | 'basic' => 'Basic', 14 | 'smtp' => 'SMTP', 15 | 16 | 'siteName' => 'Site Name', 17 | 'siteTitle' => 'Site Title', 18 | 'siteKeyword' => 'Site Keyword', 19 | 20 | 'timezone' => 'Time Zone', 21 | 'commentCheck' => 'Comment Check', 22 | 23 | 'smtpHost' => 'SMTP Host', 24 | 'smtpPort' => 'SMTP Port', 25 | 'smtpUser' => 'SMTP User', 26 | 'smtpPassword' => 'SMTP Password', 27 | 'smtpMail' => 'SMTP Mail', 28 | 29 | ]; 30 | -------------------------------------------------------------------------------- /messages/zh-CN/setting.php: -------------------------------------------------------------------------------- 1 | '创建', 5 | 'Create' => '创建', 6 | 'Update ' => '更新', 7 | 'Update' => '更新', 8 | 'Delete' => '删除', 9 | 10 | 'Setting' => '设置', 11 | 12 | 'info' => '站点', 13 | 'basic' => '基本', 14 | 'smtp' => 'SMTP', 15 | 16 | 'siteName' => '站点名称', 17 | 'siteTitle' => '站点标题', 18 | 'siteKeyword' => '站点关键字', 19 | 20 | 'timezone' => '时区', 21 | 'commentCheck' => '评论审核', 22 | 23 | 'smtpHost' => '服务器', 24 | 'smtpPort' => '端口', 25 | 'smtpUser' => '用户名', 26 | 'smtpPassword' => '密码', 27 | 'smtpMail' => '显示地址', 28 | 29 | ]; 30 | -------------------------------------------------------------------------------- /migrations/m141208_201488_setting_init.php: -------------------------------------------------------------------------------- 1 | createTable( 30 | '{{%setting}}', 31 | [ 32 | 'id' => Schema::TYPE_PK, 33 | 'parent_id' => Schema::TYPE_INTEGER . ' NOT NULL DEFAULT 0', 34 | 'code' => Schema::TYPE_STRING . '(32) NOT NULL', 35 | 'type' => Schema::TYPE_STRING . '(32) NOT NULL', 36 | 'store_range' => Schema::TYPE_STRING . '(255)', 37 | 'store_dir' => Schema::TYPE_STRING . '(255)', 38 | 'value' => Schema::TYPE_TEXT . '', 39 | 'sort_order' => Schema::TYPE_INTEGER . ' NOT NULL DEFAULT 50', 40 | ], 41 | $tableOptions 42 | ); 43 | 44 | // Indexes 45 | $this->createIndex('parent_id', '{{%setting}}', 'parent_id'); 46 | $this->createIndex('code', '{{%setting}}', 'code'); 47 | $this->createIndex('sort_order', '{{%setting}}', 'sort_order'); 48 | 49 | // Add default setting 50 | $this->execute($this->getSettingSql()); 51 | } 52 | 53 | /** 54 | * @return string SQL to insert first user 55 | */ 56 | private function getSettingSql() 57 | { 58 | return "INSERT INTO {{%setting}} (`id`, `parent_id`, `code`, `type`, `store_range`, `store_dir`, `value`, `sort_order`) VALUES 59 | (11, 0, 'info', 'group', '', '', '', '50'), 60 | (21, 0, 'basic', 'group', '', '', '', '50'), 61 | (31, 0, 'smtp', 'group', '', '', '', '50'), 62 | (1111, 11, 'siteName', 'text', '', '', 'Your Site', '50'), 63 | (1112, 11, 'siteTitle', 'text', '', '', 'Your Site Title', '50'), 64 | (1113, 11, 'siteKeyword', 'text', '', '', 'Your Site Keyword', '50'), 65 | (2111, 21, 'timezone', 'select', '-12,-11,-10,-9,-8,-7,-6,-5,-4,-3.5,-3,-2,-1,0,1,2,3,3.5,4,4.5,5,5.5,5.75,6,6.5,7,8,9,9.5,10,11,12', '', '8', '50'), 66 | (2112, 21, 'commentCheck', 'select', '0,1', '', '1', '50'), 67 | (3111, 31, 'smtpHost', 'text', '', '', 'localhost', '50'), 68 | (3112, 31, 'smtpPort', 'text', '', '', '', '50'), 69 | (3113, 31, 'smtpUser', 'text', '', '', '', '50'), 70 | (3114, 31, 'smtpPassword', 'password', '', '', '', '50'), 71 | (3115, 31, 'smtpMail', 'text', '', '', '', '50') 72 | "; 73 | } 74 | 75 | /** 76 | * @inheritdoc 77 | */ 78 | public function down() 79 | { 80 | $this->dropTable('{{%setting}}'); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /models/Setting.php: -------------------------------------------------------------------------------- 1 | 32], 40 | [['store_range', 'store_dir'], 'string', 'max' => 255] 41 | ]; 42 | } 43 | 44 | /** 45 | * @inheritdoc 46 | */ 47 | public function attributeLabels() 48 | { 49 | return [ 50 | 'id' => Module::t('setting', 'ID'), 51 | 'parent_id' => Module::t('setting', 'Parent ID'), 52 | 'code' => Module::t('setting', 'Code'), 53 | 'type' => Module::t('setting', 'Type'), 54 | 'store_range' => Module::t('setting', 'Store Range'), 55 | 'store_dir' => Module::t('setting', 'Store Dir'), 56 | 'value' => Module::t('setting', 'Value'), 57 | 'sort_order' => Module::t('setting', 'Sort Order'), 58 | ]; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /views/default/index.php: -------------------------------------------------------------------------------- 1 | title = Module::t('setting', 'Setting'); 9 | $this->params['breadcrumbs'][] = $this->title; 10 | 11 | $items = []; 12 | foreach ($settingParent as $parent) { 13 | $item['label'] = Module::t('setting', $parent->code); 14 | 15 | $str = ''; 16 | $children = Setting::find()->where(['parent_id' => $parent->id])->orderBy(['sort_order' => SORT_ASC, 'id' => SORT_ASC])->all(); 17 | foreach ($children as $child) { 18 | $str .= '
'; 19 | 20 | if ($child->type == 'text') 21 | $str .= Html::textInput("Setting[$child->code]", $child->value, ["class" => "form-control"]); 22 | elseif ($child->type == 'password') 23 | $str .= Html::passwordInput("Setting[$child->code]", $child->value, ["class" => "form-control"]); 24 | elseif ($child->type == 'select') { 25 | $options = []; 26 | $arrayOptions = explode(',', $child->store_range); 27 | foreach ($arrayOptions as $option) 28 | $options[$option] = Module::t('setting', $option); 29 | 30 | $str .= Html::dropDownList("Setting[$child->code]", $child->value, $options, ["class" => "form-control"]); 31 | } 32 | 33 | $str .= '
'; 34 | } 35 | $item['content'] = $str; 36 | 37 | array_push($items, $item); 38 | } 39 | 40 | ?> 41 | 42 | 45 | 46 |
47 | 'setting-form', 49 | 'options' => ['class' => 'form-horizontal'], 50 | 'fieldConfig' => [ 51 | 'template' => "{label}\n
{input}{hint}
\n
{error}
", 52 | 'labelOptions' => ['class' => 'col-lg-2 control-label'], 53 | ], 54 | ]); ?> 55 | 56 | $items, 59 | 'options' => ['tag' => 'div'], 60 | 'itemOptions' => ['tag' => 'div'], 61 | 'headerOptions' => ['class' => 'my-class'], 62 | 'clientOptions' => ['collapsible' => false], 63 | ]); 64 | ?> 65 | 66 |
67 | 68 | 'btn btn-primary']) ?> 69 |
70 | 71 | 72 | 73 |
74 | -------------------------------------------------------------------------------- /yii2-setting-preview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/funson86/yii2-setting/16ffd652e376a688718f465c090db461a1e1a8a0/yii2-setting-preview.png --------------------------------------------------------------------------------