yii2mod/yii2-moderation
Composer 安装命令:
composer require yii2mod/yii2-moderation
包简介
A simple Content Moderation System for Yii2 that allows you to Approve or Reject resources like posts, comments, etc.
README 文档
README
Yii2 Moderation Extension
A simple Content Moderation System for Yii2 that allows you to Approve or Reject resources like posts, comments, etc.
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist yii2mod/yii2-moderation "*"
or add
"yii2mod/yii2-moderation": "*"
to the require section of your composer.json.
Configuration
To enable moderation for a model, use the yii2mod\moderation\ModerationBehavior behavior and add the status and moderated_by columns to your model's table.
Create a migration to add the new columns. Example Migration:
<?php use yii\db\Migration; /** * Handles adding moderation columns to table `post`. */ class m161117_092603_add_moderation_columns_to_post_table extends Migration { /** * @inheritdoc */ public function up() { $this->addColumn('post', 'status', $this->smallInteger()); $this->addColumn('post', 'moderated_by', $this->integer()); } /** * @inheritdoc */ public function down() { $this->dropColumn('post', 'status'); $this->dropColumn('post', 'moderated_by'); } }
To use ModerationBehavior, insert the following code to your ActiveRecord class:
use yii2mod\moderation\ModerationBehavior; class Post extends ActiveRecord { /** * @inheritdoc */ public function behaviors() { return [ ModerationBehavior::class, ]; } }
By default, ModerationBehavior will automatically set the moderated_by attribute.
If your attribute names are different, you may configure the [[statusAttribute]] and [[moderatedByAttribute]] properties like the following:
use yii2mod\moderation\ModerationBehavior; class Post extends ActiveRecord { /** * @inheritdoc */ public function behaviors() { return [ [ 'class' => ModerationBehavior::class, 'statusAttribute' => 'status_id', 'moderatedByAttribute' => 'moderator_id', // or set to `false` to disable this attribute. ], ]; } }
Usage
In next examples I will use Post model to demonstrate how the behavior and query class works. You can moderate any ActiveRecord Model.
Moderate Models
ModerationBehavior provides the following methods for model moderation:
$post->markApproved(); // Change post status to Approved $post->markRejected(); // Change post status to Rejected $post->markPostponed(); // Change post status to Postponed $post->markPending(); // Change post status to Pending
Model Status
ModerationBehavior also provides the following methods for checking the moderation status:
$post->isPending(); // Check if a post is pending $post->isApproved(); // Check if a post is approved $post->isRejected(); // Check if a post is rejected $post->isPostponed(); // Check if a post is postponed
Events
By default [[yii2mod\moderation\ModerationBehavior]] triggers [[yii2mod\moderation\ModerationBehavior::EVENT_BEFORE_MODERATION]]
You may attach the event handlers for this event to your ActiveRecord object:
$post = Post::findOne($id); $post->on(ModerationBehavior::EVENT_BEFORE_MODERATION, function ($event) { $event->isValid = false; // prevent moderation for the model });
You may also handle these events inside your ActiveRecord class by declaring the corresponding method:
use yii2mod\moderation\ModerationBehavior; class Post extends ActiveRecord { /** * @inheritdoc */ public function behaviors() { return [ ModerationBehavior::class, ]; } public function beforeModeration() { $this->moderated_at = time(); // log the moderation date return true; } }
Query Models
ModerationQuery adds the ability of getting only approved, rejected, postponed or pending models. Usage example:
use yii2mod\moderation\ModerationQuery; class Post extends ActiveRecord { public static function find() { return new ModerationQuery(get_called_class()); } }
Now you can use the following methods:
Post::find()->approved()->all(); // It will return all Approved Posts Post::find()->pending()->all(); // It will return all Pending Posts Post::find()->rejected()->all(); // It will return all Rejected Posts Post::find()->postponed()->all(); // It will return all Postponed Posts Post::find()->approvedWithPending()->all() // It will return all Approved and Pending Posts
Support us
Does your business depend on our contributions? Reach out and support us on Patreon. All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.
yii2mod/yii2-moderation 适用场景与选型建议
yii2mod/yii2-moderation 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 93.92k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2016 年 11 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「yii2」 「moderation」 「moderation-system」 「content-moderation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yii2mod/yii2-moderation 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yii2mod/yii2-moderation 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yii2mod/yii2-moderation 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A simple Content Moderation System for Laravel 5.* that allows you to Approve or Reject resources like posts, comments, users, etc.
A custom URL rule class for Yii 2 which allows to create translated URL rules
A CakePHP plugin for adding threaded comments with moderation support
Laravel Profanity Filter - Powerful PHP package for detecting, filtering, and masking profanity in multiple languages. Supports leet speak, custom word lists, case sensitivity, and seamless Laravel integration.
Operational approvals engine for Laravel applications.
A simple moderation system for Laravel.
统计信息
- 总下载量: 93.92k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 23
- 依赖项目数: 5
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-11-17