nguyentranchung / laravel-moderation
为 Laravel 5.* 提供的简单内容审核系统,允许您批准或拒绝帖子、评论、用户等资源。
Requires
- php: >=5.4.0
Requires (Dev)
- laravel/laravel: 5.*
- nunomaduro/collision: ^2.0
- phpunit/phpunit: 7.0
README
为 Laravel 5.* 提供的简单审核系统,允许您批准或拒绝帖子、评论、用户等资源。
通过防止冒犯性、无关或不敬的内容来保持您的应用程序纯净。
可能的使用案例
-
用户创建一个资源(帖子、评论或任何 Eloquent 模型)。
-
资源处于待审状态,在网站上不可见(例如,
Post::all()只返回已批准的帖子)。 -
审核员决定资源是否将被批准、拒绝或推迟。
-
批准:资源现在是公开的且可查询。
-
拒绝:资源将不会出现在所有查询中。只有当您将查询范围缩小到包括它们时,才会返回已拒绝的资源。(范围:
withRejected) -
推迟:资源将不会出现在所有查询中,直到审核员决定批准它。
-
您的应用程序是干净的。
安装
首先,通过 Composer 安装此包。
composer require hootlex/laravel-moderation
如果您正在使用 Laravel < 5.5,则需要将 Hootlex\Moderation\ModerationServiceProvider 添加到您的 config/app.php 提供者数组中
'providers' => [ ... Hootlex\Moderation\ModerationServiceProvider::class, ... ];
最后,发布配置文件。
php artisan vendor:publish --provider="Hootlex\Moderation\ModerationServiceProvider" --tag=config
准备模型
要为一个模型启用审核,请在该模型上使用 Hootlex\Moderation\Moderatable 特性,并添加 status、moderated_by 和 moderated_at 列到您的模型表中。
use Hootlex\Moderation\Moderatable; class Post extends Model { use Moderatable; ... }
创建一个迁移来添加新列。 (您可以使用自定义名称为审核列命名)
示例迁移
class AddModerationColumnsToPostsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::table('posts', function (Blueprint $table) { $table->smallInteger('status')->default(0); $table->dateTime('moderated_at')->nullable(); //To track who moderated the Model, add 'moderated_by' and set the column name in the config file. //$table->integer('moderated_by')->nullable()->unsigned(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('posts', function(Blueprint $table) { $table->dropColumn('status'); $table->dropColumn('moderated_at'); //$table->dropColumn('moderated_by'); }); } }
您已经准备就绪!
使用方法
注意:在以下示例中,我将使用 Post 模型来演示查询构建器的用法。您可以审核任何 Eloquent 模型,甚至用户。
审核模型
您可以审核一个模型实例
$post->markApproved(); $post->markRejected(); $post->markPostponed(); $post->markPending();
或通过引用它的 id
Post::approve($post->id); Post::reject($post->id); Post::postpone($post->id);
或通过执行查询。
Post::where('title', 'Horse')->approve(); Post::where('title', 'Horse')->reject(); Post::where('title', 'Horse')->postpone();
查询模型
默认情况下,查询只会返回已批准的模型。要更改此行为,请检查 配置。
要查询已批准的帖子,请像往常一样运行查询。
//it will return all Approved Posts (strict mode) Post::all(); // when not in strict mode Post::approved()->get(); //it will return Approved Posts where title is Horse Post::where('title', 'Horse')->get();
查询待审或拒绝的模型。
//it will return all Pending Posts Post::pending()->get(); //it will return all Rejected Posts Post::rejected()->get(); //it will return all Postponed Posts Post::postponed()->get(); //it will return Approved and Pending Posts Post::withPending()->get(); //it will return Approved and Rejected Posts Post::withRejected()->get(); //it will return Approved and Postponed Posts Post::withPostponed()->get();
查询所有模型
//it will return all Posts Post::withAnyStatus()->get(); //it will return all Posts where title is Horse Post::withAnyStatus()->where('title', 'Horse')->get();
模型状态
要检查模型的状态,有三个辅助方法返回布尔值。
//check if a model is pending $post->isPending(); //check if a model is approved $post->isApproved(); //check if a model is rejected $post->isRejected(); //check if a model is rejected $post->isPostponed();
严格审核
严格审核意味着只有已批准的资源将被查询。要查询与已批准资源一起的待审资源,您必须禁用严格审核。请参阅如何在 配置 中执行此操作。
配置
全局配置
要全局配置审核包,您必须编辑 config/moderation.php。在 moderation.php 中,您可以配置以下内容
status_column代表数据库中的默认列 'status'。moderated_at_column代表数据库中的默认列 'moderated_at'。moderated_by_column代表数据库中的默认列 'moderated_by'。strict代表 严格审核。
模型配置
在您的模型中,您可以定义一些变量来覆盖 全局设置。
要覆盖 status 列定义
const MODERATION_STATUS = 'moderation_status';
要覆盖 moderated_at 列定义
const MODERATED_AT = 'mod_at';
要覆盖 moderated_by 列定义
const MODERATED_BY = 'mod_by';
启用或禁用 严格审核
public static $strictModeration = true;