定制 futuretek/yii2-options 二次开发

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

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

futuretek/yii2-options

Composer 安装命令:

composer require futuretek/yii2-options

包简介

This extension provide application configuration support stored in database.

README 文档

README

This extension provide application configuration support stored in database.

WARNING: Version 2.x introduces breaking changes. Upgrading from version 1.x is possible but requires additional actions.

Steps to migrate to version 2.x:

  • Create options config for every used option item
  • Include component in Yii2 config
  • Rewrite commands to new syntax (or use compat class futuretek\options\compat\Option)

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist futuretek/yii2-options "*"

or add

"futuretek/yii2-options": "*"

to the require section of your composer.json file.

Configuration

To start using this extension you have to load it in Yii config:

return [
    'components' => [
        'options' => [
            'class' => 'futuretek\options\Option',
            'configFile' => __DIR__ . '/options.php',
            'enableCaching' => true,
        ],     
    ],
];

The component itself can be configured with the following properties:

Property Default value Description
db db The DB connection object or the application component ID of the DB connection.
cache cache The cache object or the application component ID of the cache object. The option data will be cached using this cache object.
optionTable {{%option}} The name of the option table.
cachingDuration 0 The time in seconds that the options can remain valid in cache. Use 0 to indicate that the cached data will never expire.
enableCaching false Whether to enable options caching
configFile Options config file. This file will be required. File must return config array.
config Variable into which the config from configFile will be loaded.

Notice: Option config is lazy-loaded on init (as opposite of including it in the application config). This is because the translation engine is not available at that time.

Option config/definition array contains list of the option groups.

Option group

Every option group consists of these attributes. Attributes marked with (*) are required.

  • title (*) - Group title
  • items (*) - Contains array of option items (see below)
  • visible - If the group is visible

Option items

Option group attribute items is array containing option items.

Option item consists of these attributes. Attributes marked with (*) are required.

  • name (*) - Option name (unique)
  • type (*) - Option type. See Option::TYPE_* constants.
  • title (*) - Option title
  • hint - Option description
  • visible - If the option is visible
  • default - Specify option default value. Getting defined but unset option yields this value. When not set, null will be returned.
  • context - If the option is context option. If this is true, option will not be rendered using provided actions.
  • data - For option type=TYPE_OPTION this attribute contains data in form of:
    1. array of arrays containing keys id and name (see example below) or
    2. anonymous function returning the same array

ADVICE: If you need to create divider between options in the same option group, simply put string as one of the items array item.

Config example

return [
    [
        'title' => Yii::t('app', 'Group one'),
        'visible' => true,
        'items' => [
            ['name' => 'PHONE', 'type' => Option::TYPE_PHONE, 'title' => Yii::t('app', 'Phone number')],
            ['name' => 'EMAIL', 'type' => Option::TYPE_EMAIL, 'title' => Yii::t('app', 'Email address')],
            Yii::t('app', 'Apperance'), //Divider
            [
                'name' => 'COLOR',
                'type' => Option::TYPE_OPTION,
                'title' => Yii::t('app', 'Color scheme'),
                'data' => [
                    ['id' => 'ff0000', 'name' => 'Red'],
                    ['id' => '00ff00', 'name' => 'Green'],
                    ['id' => '0000ff', 'name' => 'Blue'],
                ],
            ],
        ],
    ],
    [
        'title' => Yii::t('app', 'Another group'),
        'visible' => YII_ENV_DEV, //Visible only in dev env
        'items' => [
            [
                'name' => 'LOGIN_TYPE',
                'type' => Option::TYPE_OPTION,
                'title' => Yii::t('app', 'Login Type'),
                'hint' => Yii::t('app', 'Please select desired login type'),
                'data' => function () {
                    return \app\classes\Tools::findAvailableLoginProviders();
                },
            ],
        ],
    ],
];

Usage

This extension creates options component for storing configuration.

Getting options

$value = \Yii::$app->options->get('OPTION_NAME');

Setting options

\Yii::$app->options->set('OPTION_NAME', $value);

Context options

Context comes handy in case you need to deal with storing different configuration for multiple entities (for example user configuration).

In this use case context can be uses. Simply specify entity identificator as context and entity record id as context_id and you are done.

Getting and setting options is done via another arguments of methods get() and set().

$userValue = \Yii::$app->options->get('OPTION_NAME', self::class, $this->getPrimaryKey());
\Yii::$app->options->set('OPTION_NAME', $userValue, self::class, $this->getPrimaryKey());

Rendering settings page

If you want to render settings page, there is futuretek\options\IndesAction provided. You can simply add it to the controller actions() method.

There is also futuretek\options\OptionsHelper that provides method for rendering option edit field based on option type.

See mentioned files for more details.

Development

Translations

Translations are managed trough standard Yii2 translations. To automatically register extension translations we recommend to use our another extension futuretek/yii2-composer.

To generate messages for translating run the following command in extension root directory.

yii message/extract messages/config.php

Assets

Assets are managed by Compass

  • While developing run compass watch in extension root directory
  • To compile assets for final distribution run compass compile -e production --force in extension root directory.

futuretek/yii2-options 适用场景与选型建议

futuretek/yii2-options 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12.68k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 09 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「options」 「extension」 「component」 「yii」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 futuretek/yii2-options 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 futuretek/yii2-options 我们能提供哪些服务?
定制开发 / 二次开发

基于 futuretek/yii2-options 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2017-09-14