hootlex / 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();
模型状态
要检查模型的状态,有 3 个辅助方法,它们返回布尔值。
//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;