alayubi / laravel-comment
为您的Laravel应用添加评论。
Requires
- php: ^7.3|^8.0
This package is not auto-updated.
Last update: 2024-09-26 11:21:03 UTC
README
composer require alayubi/laravel-comment
您必须使用
php artisan vendor:publish --tag=lara-comment-migrations
发布迁移,并重新运行迁移。
配置
您可以使用
php artisan vendor:publish --tag=lara-comment-config
用法
可评论
如果您想让一个模型可以评论,可以实现 \Lara\Comment\Contracts\IsCommentable
接口,并为实现添加 \Lara\Comment\Commentable
特性。
namespace App\Models; use Illuminate\Database\Eloquent\Model; use Lara\Comment\Commentable; use Lara\Comment\Contracts\IsCommentable; class Post extends Model implements IsCommentable { use Commentable; }
评论者
评论者是评论模型的模型。如果您想让您的模型成为评论者,可以在模型上实现 \Lara\Comment\Contracts\IsCommentator\
接口,并为实现添加 \Lara\Comment\Commentator
特性。
namespace App\Models; use Illuminate\Database\Eloquent\Model; use Lara\Comment\Commentator; use Lara\Comment\Contracts\IsCommentator; class User extends Model implements IsCommentator { use Commentator; }
创建评论
要创建一个模型的评论,您应该首先创建带有表单的视图。
<form action="/posts/comments/store" methdo="POST"> <textarea name="comment"></textarea> <button type="submit">Submit</button> </form>
至少您需要提供带有 comment
名称的 textarea 标签 HTML。然后,您可以创建一个路由来处理请求。在您的控制器中,您可以使用 \Lara\Comment\CommentService
类和 store
方法来创建评论。
$post = Post::find(1); $user = Auth::user(); $comment = CommentService::for($post, $user) ->store();
更新评论
$commentToUpdate = Comment::find(1); $user = Auth::user(); $comment = CommentService::for($commentToUpdate, $user) ->update();
销毁评论
$post = Post::find(1); $user = Auth::user(); $comment = CommentService::for($post, $user) ->destroy();
前端
在您开始使用默认前端之前,您必须将您的应用与
- vue
- tailwindcss
以下命令将发布嵌套评论前端。使用嵌套评论,您可以对评论进行回复。您可以使用以下命令发布前端:
php artisan vendor:publish --tag=lara-comment-vue
它将在资源视图的 vendor/comment 目录中创建视图,并在资源/JS/components/comment 目录中创建 Vue 组件。不要忘记将以下代码复制到您的 app.js 中。
Vue.component('edit-comment', require('./components/comment/EditComment.vue').default); Vue.component('reply-comment', require('./components/comment/ReplyComment.vue').default);
发布后,您将能够自定义 CSS 以适应您的视图。要使用嵌套评论前端,您可以将它包含到您的视图中,并将可评论的模型传递给它。
@include('vendor.comment.comment-list', ['commentable' => $post])
该代码将渲染属于 $post
的嵌套缩进的评论。通过在配置文件中更改 indentation
,您可以在更深层的缩进中回复评论。您可以想象缩进如下:
- 0
- 1
- 2
路由
默认情况下,有三个路由用于常见任务。
- 创建访问
route('comments.comments.store')
或/comments/{comment}/comments
,使用 POST 方法在评论上创建评论。 - 更新访问
route('comments.update')
或/comments/{comment}
,使用 PUT 方法更新评论。 - 销毁访问
route('comments.destroy')
或/comments/{comment}
,使用 DELETE 方法从存储中删除评论。
如果您不想使用默认路由,在设置文件 comment.php 中将 route
中的值设置为 false
。
'route' => false
验证规则和请求数据
\Lara\Comment\Validation\DefaultValidator
是默认验证器。验证器是一个负责从用户获取和验证数据的类。
如果您想更改默认验证行为或要存储哪种类型的数据,可以扩展 \Lara\Comment\Validation\Validator
抽象类,然后您必须实现 public function data() 和 public function rules() 方法。您可以从该类访问评论者模型和请求对象。
data()
data 函数负责返回要存储的数据类型。
public function data() { return [ 'user_id' => $this->commentator->id, 'comment' => $this->request->get('comment'), ]; }
rules()
rules 函数负责定义要运行的验证规则。
public function rules() { return [ 'user_id' => 'required', 'comment' => 'required', ]; }
不要忘记将配置验证器值更改为您的实现。
return [ 'validator' => \Lara\Comment\Validation\DefaultValidator::class, ]
validateWithBag() 方法
如果您有多个评论表单,并且想要显示错误消息,可以使用 validateWithBag()
方法进行带包的验证。
$commentToUpdate = Comment::find(1); $user = Auth::user(); $comment = CommentService::for($commentToUpdate, $user) ->validateWithBag() ->update();
当验证错误发生时,您可以访问错误包。
{{ $errors->{$comment->id . 'PUT'}->first('comment') }}
您可以通过组合commentable
ID和方法PUT
、POST
来访问名称错误包。
重定向器
当验证失败时,重定向器将重定向到URL。默认的重定向器是\laravel\Comment\Redirect\RedirectBack。这将带有URL片段#validation-comment-error的重定向回。如果您想更改此默认行为,可以创建自己的重定向,通过扩展\laravel\Comment\Redirect\Redirect抽象类,并在配置文件中更改重定向器值为您自己的实现。
return [ 'redirector' => \Lara\Comment\Redirect\RedirectBack::class ];
策略
您可以创建自己的策略来授权操作。要创建策略类,只需运行laravel artisan命令。有关完整指南,请参阅laravel文档。
php artisan make:policy CommentPolicy
别忘了更改配置文件中的policy
类。
return [ 'policy' => \Lara\Comment\CommentPolicy::class ]