kit-tools/yii2-tags
Composer 安装命令:
composer require kit-tools/yii2-tags
包简介
Tags extensions
README 文档
README
The extension allows you to add tags to different models. Counts the weight of tags and the number of clicks per tag for each model. There is also an admin panel for adding tags. And a few widgets to display tags.
Installation
The preferred way to install this extension is through composer.
Either run:
composer require kit-tools/yii2-tags
or add
"kit-tools/yii2-tags": "^1.0.0"
to the require section of your composer.json file.
Usage
Admin
Add to config (config/main.php или config/web.php)
'controllerMap' => [ 'tag' => 'kittools\tags\controllers\TagController' ],
Property $tagList
In the model with which you want to associate tags, add:
<?php ... /** * @var array list of tags entered by the user. */ public $tagList = []; ...
Validation rules
<?php public function rules(){ ... ['tagList', 'required', 'skipOnEmpty' => false], ['tagList', 'each', 'rule' => ['string'], 'message' => 'Tags are filled incorrectly.'], ... }
If the tags are optional, then you need to delete the line
['tagList', 'required'],
Behavior
public function behaviors() { return [ ... 'tagBehavior' => [ 'class' => TagBehavior::className(), ], ... ]; }
TagBehavior works with events:
- ActiveRecord :: EVENT_AFTER_INSERT, ActiveRecord :: EVENT_AFTER_UPDATE - adds new tags, establishes / removes model and tag relationships.
- ActiveRecord :: EVENT_AFTER_DELETE - when deleting a model, removes links with tags. Linked tags are not removed.
Important!!! If there are connected tags in the model, and the tags will not be transferred when saving, then all links with the tags will be deleted! To avoid this situation, before saving the model, you need to untie the TagBehavior
$model->detachBehavior('tagBehavior');
One-to-many relationship
<?php use kittools\tags\models\Tag; use kittools\tags\models\TagEntityRelation; ... public function getTags() { return $this->hasMany(Tag::class, ['id' => 'tag_id']) ->viaTable(TagEntityRelation::tableName(), ['entity_id' => 'id'], function($query){ $query->andWhere([TagEntityRelation::tableName() . '.entity' => static::class]); }); }
The method adds a model link to odic-to-many tags through an intermediate table.
Form adding tags in creating / editing models
Add tag widget
<?php use kittools\tags\widgets\TagInputWidget; ... ?> ... <?= $form->field($model, 'tagList') ->widget(TagInputWidget::className(), [ 'pluginOptions' => [ // maximum number of tags that can be selected 'maximumSelectionLength'=> 5, ] ]) ->hint('Press "Space", "," or Enter to separate tags from each other.') ->label('Tags'); ?> ...
The widget loads the tags with an ajax request. Minimum word length after which tags will be loaded, 2 characters.
or
<?php use kittools\tags\widgets\TagInputWidget; use yii\helpers\ArrayHelper; use kittools\tags\models\Tag; ... ?> ... <?= $form->field($model, 'tagList') ->widget(TagInputWidget::className(), [ 'pluginOptions' => [ // maximum number of tags that can be selected 'maximumSelectionLength'=> 2, // Minimum tag length for listing tags, default 2 'minimumInputLength' => 0, // disabling tag loading by ajax request 'ajax' => null, ], // Tags list 'data' => ArrayHelper::map(Tag::find()->all(), 'title', 'title') ]) ->hint('Press "," or "Enter" to separate tags from each other') ->label('Tags'); ?> ...
The widget does not load tags through an ajax request. A list of tags is loaded when the page loads.
Tag widget inherited from kartik\select2\Select2.
More information about widget settings can be found at https://select2.org/
Tag output widget
When using the widget in the list of materials, do not forget about the "problem of N + 1 queries".
<?php use kittools\tags\widgets\TagsEntityWidget; ... echo TagsEntityWidget::widget([ 'model' => $model, 'caseRegister' => 'upper', 'tagUrl' => ['site/tag'], 'delimiter' => ', ', ]); ...
Detailed description of widget settings in a file kittools\tags\widgets\TagsEntityWidget
Tag cloud widget
<?php use kittools\tags\widgets\CloudTagsWidget; use app\models\News; ... echo CloudTagsWidget::widget([ 'entity' => News::className(), 'type' => CloudTagsWidget::TYPE_WEIGHT, 'shuffleTags' => false, 'tagUrl' => ['/news/tag'], 'coefficientIncreaseFont' => 1, 'tagPrefix' => '', 'tagUrlOptions' => ['style' => 'color: #003366;', 'target' => '_blank'] ]); ...
more information about widget settings can be found in kittools\tags\widgets\CloudTagsWidget
kit-tools/yii2-tags 适用场景与选型建议
kit-tools/yii2-tags 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 30 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 08 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「tags」 「yii2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 kit-tools/yii2-tags 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kit-tools/yii2-tags 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 kit-tools/yii2-tags 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A powerfull cacher based on laravels file cache driver
A custom URL rule class for Yii 2 which allows to create translated URL rules
Phalcon PHP Meta tags service
A tool for scraping URL resources (oEmbed, OpenGraph, Twitter cards, JSON-LD)
The tag extension for the yii framework
Colorizes the Flarum forum header and interface based on the active tag's color.
统计信息
- 总下载量: 30
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2019-08-26