yii2mod/yii2-moderation 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

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.

Latest Stable Version Total Downloads License Build Status

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 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 7
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-11-17