tizis / lara-comments
Laravel 应用程序的评论系统。功能:可用于对任何模型进行评论,HTML 过滤器自定义(HTMLPurifier),API,评论评分,回复,事件,认证规则...
Requires
- php: ^7.1|^8.0
- illuminate/database: ~5.6.0|~5.7.0|~5.8.0|^6.0|^7.0|^8.0|^9.0|^10.0
- illuminate/http: ~5.6.0|~5.7.0|~5.8.0|^6.0|^7.0|^8.0|^9.0|^10.0
- illuminate/support: ~5.6.0|~5.7.0|~5.8.0|^6.0|^7.0|^8.0|^9.0|^10.0
- mews/purifier: ^2.1|^3.0
Requires (Dev)
- orchestra/testbench: ~3.0
- phpunit/phpunit: ^7.0|^9.5
- dev-master
- V4.1.0
- v4.0.1
- v4.0.0
- 3.0.5
- 3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.1.0
- v2.0.2
- v2.0.1
- v2.0
- v1.19.1
- v1.19
- v1.18.1
- v1.18
- v1.17
- v1.16
- v1.15
- 1.14.2
- 1.14.1
- 1.14.0
- 1.10.0
- 1.9.0
- 1.8.6
- 1.8.2
- 1.8.1
- 1.8.0
- 1.7.1
- 1.7.0
- 1.6.0
- 1.5.1
- 1.5.0
- 1.4.2
- 1.4.1
- 1.4.0
- v1.3.0
- v1.2.0
- v1.1.5
- v1.1.4
- v1.1.3
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
This package is auto-updated.
Last update: 2024-09-09 19:00:28 UTC
README
laraComments
此包可用于对应用程序中的任何模型进行评论。
功能
- 查看评论
- 创建评论
- 删除评论
- 编辑评论
- 回复评论
- 认证规则 | 自定义
- 查看自定义
- 派发事件
- 点赞 | 点踩 | 评论评分
- 基本功能API:获取、更新、删除、创建
- HTML 过滤器自定义(使用 HTMLPurifier)
升级指南 →
要求
- php 7.1 +
- laravel 5.6 +
您的应用程序应包含认证模块。
- Laravel 6.x: https://laravel.net.cn/docs/6.x/authentication
- Laravel 5.6: https://laravel.net.cn/docs/5.6/authentication
安装
composer require tizis/lara-comments
1. 运行迁移
我们需要创建评论的表。
php artisan migrate
2. 将 Commenter 特性添加到您的 User 模型
将 Commenter
特性添加到您的 User 模型,以便您可以检索用户的评论
use tizis\laraComments\Traits\Commenter; class User extends Authenticatable { use ..., Commenter;
3. 创建 Comment 模型
use tizis\laraComments\Entity\Comment as laraComment; class Comment extends laraComment { }
4. 将 Commentable
特性和 ICommentable
接口添加到模型
将 Commentable
特性和 ICommentable
接口添加到您想启用评论的模型
use tizis\laraComments\Contracts\ICommentable; use tizis\laraComments\Traits\Commentable; class Post extends Model implements ICommentable { use Commentable;
5. 自定义评论策略(可选)
如果您需要,您可以覆盖默认的评论策略类
<?php namespace App\Http\Policies; use App\Entity\Comment; use tizis\laraComments\Policies\CommentPolicy as CommentPolicyPackage; class CommentPolicy extends CommentPolicyPackage { // overwrite delete rule public function delete($user, $comment): bool { // ever true return true; } }
然后在 AuthServiceProvider
中注册策略
use Illuminate\Support\Facades\Gate; use App\Http\Policies\CommentPolicy; ... public function boot() { Gate::resource('comments_custom', CommentPolicy::class, [ 'delete' => 'delete', 'reply' => 'reply', 'edit' => 'edit', 'vote' => 'vote', 'store => 'store' ]); }
并在 comments.php 配置中添加策略前缀
'policy_prefix' => 'comments_custom',
发布配置 & 配置(可选)
在 config
文件中您可以指定
- 您的 User 模型位置;默认为
\App\User::class
- 您的 Comment 模型位置;默认为
\App\Comment::class
- 策略前缀,您可以创建自定义策略类并扩展
tizis\laraComments\Policies\CommentPolicy;
- 允许 HTML 过滤器的标签
- API 前缀
发布配置文件(可选)
php artisan vendor:publish --provider="tizis\laraComments\Providers\ServiceProvider" --tag=config
发布视图(自定义)
默认 UI 是为 Bootstrap 4
制作的,但您可以随意更改。
⚠⚠⚠⚠警告⚠⚠⚠⚠
所有视图示例都包含 js/css 文件以确保正确运行。存在与您的脚本和样式发生冲突的可能性。
php artisan vendor:publish --provider="tizis\laraComments\Providers\ServiceProvider" --tag=views
使用方法
1. 后端渲染
在您想显示评论的视图中放置此代码并进行修改
laravel 6
@comments(['model' => $post]) @endcomments
Laravel 7
<x-comments :model="$post"/>
在上面的示例中,我们将参数 model
设置为书籍模型的类。
幕后,该包会检测是否有当前登录的用户。
如果您打开包含上述代码的视图页面,您应该看到一个工作的评论表单。
2. 前端渲染(API)
3. 访问评论服务
如果您不想使用内置功能:API 或 CommentController,但想访问内置功能 - 您可以使用 tizis\laraComments\UseCases\CommentService
CommentService
类用于默认评论控制器中的请求处理。
要默认禁用 API 路由,请设置 route.root => null
配置值。
方法:
- 创建评论:
CommentService::createComment
$user = Auth::user();
$modelId = decrypt($request->commentable_encrypted_key)['id']; // get model id from encrypted model key
$model = $model = Post::findOrFail($modelId);
$message = '123'
$parent = rand(1, 100); // optional
$createdComment = CommentService::createComment(new Comment(), $user, $model, $message, [optional $parent]);
- 删除评论:
CommentService::deleteComment
$comment = Comment::findOrFail(123);
CommentService::deleteComment($comment);
- 更新评论:
CommentService::updateComment
$comment = Comment::findOrFail(123);
$message = 'new text';
$updatedComment = CommentService::updateComment($comment, $message);
事件
此包通过触发事件来通知您何时发生某些操作。
tizis\laraComments\Events\CommentCreated
tizis\laraComments\Events\CommentUpdated
tizis\laraComments\Events\CommentDeleted
API 预处理
⚠ 警告!仅适用于 API! ⚠
get api 属性支持的预处理器
- user [对象]
- comment [字符串]
1. 描述
有时在通过 API 传输内容之前,需要对其进行额外的处理。
2. 配置
'api' => [
'get' => [
'preprocessor' => [
'comment' => App\Helpers\CommentPreprocessor\Comment::class,
'user' => App\Helpers\CommentPreprocessor\User::class
...
]
]
]
3. 合同
创建预处理器类并实现 ICommentPreprocessor
接口
示例:
评论
namespace App\Helpers\CommentPreprocessor;
use tizis\laraComments\Contracts\ICommentPreprocessor;
class Comment implements ICommentPreprocessor
{
public function process($comment): array
{
return 'Hi, ' . $comment . '!';
}
}
用户
namespace App\Helpers\CommentPreprocessor;
use tizis\laraComments\Contracts\ICommentPreprocessor;
class User implements ICommentPreprocessor
{
public function process($user): array
{
$user->name = $user->name . '[Moderator]'
return $user;
}
}
4. 示例
无预处理
$comment = 1;
echo $comment; // 1
$user = Auth::user();
echo $user->name; // user1
有预处理
$comment = 1;
echo $comment; // Hi, 1 !
$user = Auth::user();
echo $user->name; // user1[Moderator]
可评论模型的特性
- 使用 withCommentsCount() 范围
示例
/**
* Add comments_count attribute to model
*/
Posts::withCommentsCount()->orderBy('id', 'desc')->get()
静态辅助工具
使用 tizis\laraComments\Http\CommentsHelper;
方法
- 获取最新评论(默认 $take = 10,默认 $commentable_type = null)
- 获取评论者评分(int $userId,[可选 Carbon $cacheTtl])
- 将评论移动到(CommentInterface $comment,ICommentable $newCommentableAssociate)
- 将评论移动到并删除根关联的父关联(CommentInterface $comment,ICommentable $newCommentableAssociate)
示例
CommentsHelper::getNewestComments(20) // Return last 20 comments
CommentsHelper::getNewestComments(20, Book::class) // Return last 20 comments of Book model
示例
此存储库仅包括 bootstrap4
模板,但您可以创建自己的 UI。这只是包功能的示例。
这是 backend
渲染的示例,当帖子上有 100+ 条评论时,由于需要检查每条评论的用户权限(回复、编辑、删除等),因此这种方式性能较差。
一个好主意
是使用 API 并使用 Vue js(或任何其他库)在前端构建 UI,并验证用户权限(仅限 UI)。
升级指南
从 2.x.x 到 3.0
commentable_type
和 commentable_id
请求属性已合并为单个 commentable_encrypted_key
您需要替换这些已弃用的属性。
示例
Old /bootstrap4/form.blade.php
<input type="hidden" name="commentable_type" value="\{{ get_class($model) }}"/>
<input type="hidden" name="commentable_id" value="{{ $model->id }}"/>
New /bootstrap4/form.blade.php
<input type="hidden" name="commentable_encrypted_key" value="{{ $model->getEncryptedKey() }}"/>