strangefate / commenthandler
为 Laravel 模型添加评论和点赞的服务提供商。
Requires
- php: >=7.2.5
- laravel/framework: >=5.6
This package is auto-updated.
Last update: 2024-09-09 01:14:36 UTC
README
此评论处理器是一个基本的包,用于快速添加评论和点赞到您的项目。它包括一个粗俗语过滤器,用户可以举报不适当的内容。达到配置的举报阈值的内容将自动从网站上暂停。
包安装
需要包
composer require strangefate/commenthandler
运行迁移以将必要的评论、点赞和举报表添加到数据库
php artisan migrate
在 app\User.php 中添加用户及其评论的关系
use StrangeFate\CommentHandler\Comment;
...
public function comments() {
return $this->hasMany(Comment::class);
}
配置
默认情况下,所有评论选项(点赞、举报和回复评论)都已开启。发布配置文件以配置您想要的平台。
php artisan vendor:publish --provider=StrangeFate\CommentHandler\CommentHandlerServiceProvider --tag=config
这将发布评论配置文件到您的配置目录
app\config\comments.php
一旦您设置了配置,您需要将评论或点赞功能添加到您的模型中。只需进入您的模型,添加 HasComment 或 HasLike 接口以附加这些功能。
<?php
namespace App;
use StrangeFate\CommentHandler\Interfaces\HasLikes;
use StrangeFate\CommentHandler\Interfaces\HasComments;
class MyNewClass extends Model
{
use HasComments, HasLikes;
}
这将添加您模型接受评论和/或点赞所需的所有必要关系。评论将自动内置点赞的路线,但您需要创建路线和逻辑,以便您的模型可以接受它们。
Route::post('mynewclasses/{mynewclass}/comment', function(Request $request, MyNewClass $mynewclass) {
$comment = $request->validate(['message' => 'required']);
$comment['user_id'] = auth()->id();
$mynewclass->comments()->create($comment);
return redirect("/mynewclass/$mynewclass->id");
});
Route::post('mynewclasses/{mynewclass}/like', function(Request $request, MyNewClass $mynewclass) {
$mynewclass->like( auth()->id() ); //The like() function will detect if the user has already liked the item
return redirect("/mynewclass/$mynewclass->id");
});
评论自动接受点赞和适当的路由逻辑。如果您不希望在评论中显示点赞功能,您可以在 config\comments.php 文件中将其关闭。
视图模板
评论处理器附带一些预制的模板,供您在项目中包含。
@include('comment::comments', ['comments' => $module->comments->show()->get(), 'url' => 'post/route/for/submitting/comments']) {-- A general output for comments --}
@include('comment::comment_list', ['comments' => $comments] ) {-- for defining the format in which post are laid out. --}
@include('comment::create', ['url' => 'post/route/for/submitting/comments']) {-- The form for submitting a comment --}
@include('comment::schema', ['comments' => $comments]) {-- The schema.org json for dumping comments into your page for scrapping --}
您可以将视图发布到 views\vendor 文件夹以自定义网站的视图
php artisan vendor:publish --provider=StrangeFate\CommentHandler\CommentHandlerServiceProvider --tag=views
发布视图还将导出一个包含模板中所有预定义类的 sass 文件,供您为网站设计评论。只需将其包含在您的 app.scss 中,并添加您的 css。
@import "comments";
举报帖子
此模块包含一些逻辑,允许用户通过举报不适当的内容来管理论坛。您可以通过在发布后设置 config\comments.php 文件中的报告选项为 false 来完全禁用此功能。
您可以通过配置同一配置文件中的报告数组来设置举报的阈值,以自动隐藏帖子。
'report' => [
'auto_hide' => true, //automatically hide a comment after it his been reported so many times
'report_limit' => 10 //the number of times a post can be reported before it is automatically hidden
],
您可以使用查询作用域检查举报的评论,并通过其关系方法获取举报评论的用户列表。
$comments = Comment::reported()->get() //gets a list of all comments that have been reported.
$comments->first()->reports()->pluck('name') //gets a list of users that have reported the comment.
粗俗语过滤器
评论处理器附带一个粗俗语过滤器,用于隐藏可能出现在评论中的粗俗语言。您可以通过在 config\comments.php 中的诅咒数组中添加单词来扩展此列表。
'curses' => [
['curse' => 'badword', 'filter' => 'replacement'],
],
粗俗语过滤器还将检测通过用符号替换字符来绕过滤器的尝试。您可以通过在 config\comment.php 中的别名数组中添加正则表达式来扩展此列表,以捕获更多字符。
'alias' => [
'a' => '[a|@]',
'i' => '[i|!]',
'o' => '[o|0]',
's' => '[s|\$]',
'#' => '+[\s\W_]?' //spaced curse words. DO NOT REMOVE!
]
上述数组可以捕获字符串 'b@dword'、'badw0rd'、'b@dw0rd'、'b a d w o r d' 以及更多。
您可以在项目中任何需要过滤潜在粗俗语言的地方使用粗俗语过滤器。
<div>
{{ \StrangeFate\CommentHandler\Interfaces\CommentHelperFunctions::profanity_filter( $filtertext ) }}
</div>