bupy7/yii2-config

此包已被废弃,不再维护。未建议替代包。

这是一个模块,允许将应用程序的配置参数存储到数据库中,并从管理面板动态管理它们。

安装数: 2,191

依赖项: 1

建议者: 0

安全: 0

星标: 17

关注者: 2

分支: 10

开放问题: 0

类型:yii2-extension

1.1.0 2017-01-18 09:52 UTC

This package is auto-updated.

Last update: 2021-06-11 07:12:39 UTC


README

这是一个模块,允许将应用程序的配置参数存储到数据库中,并从管理面板动态管理它们。

安装

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

运行以下命令之一

php composer.phar require --prefer-dist bupy7/yii2-config "*"

"bupy7/yii2-config": "*"

将以下内容添加到您的 composer.json 文件的 require 部分中。

使用方法

添加到主应用程序配置中

'bootstrap' => [
    ...

    'config',

    ...
],
'modules' => [
    'config' => [
        'class' => 'bupy7\config\Module',
        'enableCaching' => !YII_DEBUG,
        'as access' => [
            'class' => AccessControl::className(),
            'rules' => [
                [
                    'allow' => true,
                    'roles' => ['admin'],
                ],
            ],
        ],
    ],
],
'components' => [
    'configManager' => [
        'class' => 'bupy7\config\components\ConfigManager',
    ],
],

运行迁移

./yii migrate/up --migrationPath=@bupy7/config/migrations 

安装演示参数

./yii config/init

允许通过 URL 使用配置管理器

config/default/index

添加配置参数

所有配置参数 必须 包含以下选项

  • module (字符串) - 将在其中使用的模块参数的名称(app, shop, cart, blog, news 等)。
  • name (字符串) - 参数的名称(mainPageTitle, adminEmail 等)。

模块名称和名称必须是唯一的。

  • label (字符串) - 参数的标签。它必须是翻译消息。更多信息请参阅 Yii::t()
  • type (整数) - 字段的类型(bupy7\config\Module::TYPE_INPUTbupy7\config\Module::TYPE_TEXT 等)。可以在 bupy7\config\Module 中看到允许的字段类型。
  • rules (数组) - 字段的规则。必须指定所有规则,而不包括字段名称。示例
'rules' => [
    ['required'],
    ['string', 'max' => 255],
], 

更多信息请参阅 bupy7\config\models\Config::afterFind()

附加选项

  • language (字符串) - 将使用此配置参数的语言 ('ru', 'en' 等)。如果语言是 bupy7\config\Module::LANGUAGE_ALL 或未设置,则此参数将用于所有语言。更多信息请参阅 yii\console\Application::$language|yii\web\Application::$language
  • value (字符串) - 配置参数的值。默认为空。
  • options (数组) - 依赖于字段类型。更多信息请参阅 bupy7\config\widgets\ActiveForm::field()。例如,对于 textInput 类型
'options' => [
    ['maxlength' => true]
],
  • hint (字符串) - 字段的提示。它必须是翻译消息。更多信息请参阅 Yii::t()

示例配置参数

use bupy7\config\Module as ConfigModule;

...
'modules' => [

    ...

    'config' => [
        'class' => 'bupy7\config\Module',
        'enableCaching' => !YII_DEBUG,
        'as access' => [
            'class' => AccessControl::className(),
            'rules' => [
                [
                    'allow' => true,
                    'roles' => ['admin'],
                ],
            ],
        ],
        'params' => [
            [
                'module' => 'app', 
                'name' => 'backendSitename', 
                'label' => 'PARAM_BACKEND_SITENAME', 
                'value' => 'Backend', 
                'type' => ConfigModule::TYPE_INPUT, 
                'language' => 'en',
                'rules' => [
                    ['required'],
                    ['string', 'max' => 255],
                ], 
                'options' => [
                    ['maxlength' => true]
                ],
            ],
            [
                'module' => 'app', 
                'name' => 'frontendSitename', 
                'label' => 'PARAM_FRONTEND_SITENAME', 
                'value' => 'Frontend', 
                'type' => ConfigModule::TYPE_INPUT, 
                'language' => 'en',
                'rules' => [
                    ['required'],
                    ['string', 'max' => 255],
                ], 
                'options' => [
                    ['maxlength' => true]
                ],
            ],            
            [
                'module' => 'app', 
                'name' => 'displaySitename', 
                'label' => 'PARAM_DISPLAY_SITENAME', 
                'value' => '0', 
                'type' => ConfigModule::TYPE_YES_NO, 
                'language' => ConfigModule::LANGUAGE_ALL, 
                'rules' => [
                    ['boolean'],
                ], 
                'hint' => 'HINT_PARAM_DISPLAY_SITENAME',
            ],
            [
                'module' => 'app', 
                'name' => 'supportEmail', 
                'label' => 'PARAM_SUPPORT_EMAIL', 
                'value' => 'support@support.com', 
                'type' => ConfigModule::TYPE_INPUT, 
                'language' => ConfigModule::LANGUAGE_ALL, 
                'rules' => [
                    ['required'],
                    ['email'],
                ],
            ],
            [
                'module' => 'app', 
                'name' => 'supportNameEmail', 
                'label' => 'PARAM_SUPPORT_NAME_EMAIL', 
                'value' => 'Support of site', 
                'type' => ConfigModule::TYPE_INPUT, 
                'language' => 'en', 
                'rules' => [
                    ['required'],
                    ['string', 'max' => 255],
                ],
                'options' => [
                    ['maxlength' => true]
                ],
            ],
        ],
    ],

    ...

],

添加配置参数后运行重新扫描

./yii config/rescan

结果

Screenshot

获取配置参数的值

Yii::$app->configManager->get('exampleModuleName', 'exampleParameterName');

如果找不到参数,则将抛出异常。

设置配置参数的值

Yii::$app->configManager->set('exampleModuleName', 'exampleParameterName', 'exampleParameterValue');

如果找不到参数,则将抛出异常。

控制台命令

默认情况下,命令名称等于模块名称(模块名称 - config,命令名称 - config)。

通过删除旧参数初始化应用程序的配置参数

./yii config/init

添加新的和删除不存在于应用程序中的配置参数

./yii config/rescan

添加/删除参数后,将清除缓存。

##许可证

yii2-config 在 BSD 3-Clause 许可证下发布。