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,
nullwill 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:
- array of arrays containing keys id and name (see example below) or
- 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 watchin extension root directory - To compile assets for final distribution run
compass compile -e production --forcein 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 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 futuretek/yii2-options 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Lightweight configuration interfaces and utilities
FSi DataSource Component
A custom URL rule class for Yii 2 which allows to create translated URL rules
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
Array wrapper that eases the retrieval of values. Has parameters, options and settings and traits for inclusion in other libraries.
TYPO3 CMS extension to create gallery content element with preset crop ratios and pagination
统计信息
- 总下载量: 12.68k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2017-09-14