定制 alayubi/laravel-comment 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

alayubi/laravel-comment

Composer 安装命令:

composer require alayubi/laravel-comment

包简介

Add comment to your Laravel app.

README 文档

README

composer require alayubi/laravel-comment

you must publish the migration with:

php artisan vendor:publish --tag=lara-comment-migrations

and rerun the migration.

Config

You can publish the config file with:

php artisan vendor:publish --tag=lara-comment-config

Usage

Commentable

If you want a model can be commented you can implements \Lara\Comment\Contracts\IsCommentable interface and add \Lara\Comment\Commentable trait for the implementation.

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;
}

Commentator

Commentator is a model that comment on a model. Implements \Lara\Comment\Contracts\IsCommentator\ interface on a model if you want to your model to be a commentator and add \Lara\Comment\Commentator trait for the implementation.

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;
}

Creating Comments

To create a comment to a model you should first create the view with form.

<form action="/posts/comments/store" methdo="POST">
    <textarea name="comment"></textarea>
    <button type="submit">Submit</button>
</form>

at least you provide the textarea tag HTML with comment name. Then you may create a route to handle the request. In your controller you can use \Lara\Comment\CommentService class and store method to create a comment.

$post = Post::find(1);

$user = Auth::user();

$comment = CommentService::for($post, $user)
            ->store();

Updating comment

$commentToUpdate = Comment::find(1);

$user = Auth::user();

$comment = CommentService::for($commentToUpdate, $user)
            ->update();

Destory comment

$post = Post::find(1);

$user = Auth::user();

$comment = CommentService::for($post, $user)
            ->destroy();

Frontend

Before you start with the default frontend you must integrate your app with:

  1. vue
  2. tailwindcss

The command below will publish the nested comment frontend. With nested comment you can reply a comment. You can publish the frotend with:

php artisan vendor:publish --tag=lara-comment-vue

it will create views in resources/views/vendor/comment and vue component in resources/js/components/comment directory. Don't forget to copy this code below to your app.js.

Vue.component('edit-comment', require('./components/comment/EditComment.vue').default);
Vue.component('reply-comment', require('./components/comment/ReplyComment.vue').default);

After published you are be able to customise the css to fit with your view. To use the frontend nested comment you may include it in your view and pass the commentable model to it.

@include('vendor.comment.comment-list', ['commentable' => $post])

The code will render the comments with nested indentation belongs to $post. Change the indentation in config file so that you can reply a comment in more deeper indentation. You can imagine the indentation like:

- 0
    - 1
        - 2

Routes

By default there are three routes for common task.

  1. create visit route('comments.comments.store') or /comments/{comment}/comments with POST method to create a comment on the comment.
  2. update visit route('comments.update') or /comments/{comment} with PUT method to update the comment.
  3. destroy visit route('comments.destroy') or /comments/{comment} with DELETE method to remove the comment from storage.

If you don't want to use the default route put false value in route inside setting file comment.php.

'route' => false

Validation Rule and Requested Data

\Lara\Comment\Validation\DefaultValidator is the default validator. Validator is a class that responsible to get and validate the data from user.

If you want to chnage default behavior of validation or what kind of data you will store to storage you can extends \Lara\Comment\Validation\Validator abstract class then you must implement public function data() and public function rules(). From the class you can access commentator model and request object.

data()

The data function is resposible to return what kind of data to store.

public function data()
{
    return [
        'user_id' => $this->commentator->id,
        'comment' => $this->request->get('comment'),
    ];
}

rules()

The rules function is responsible to define what kind of validation rules to run.

public function rules()
{
    return [
        'user_id' => 'required',
        'comment' => 'required',
    ];
}

Don't forget to change the config validator value to your implementation.

return [
    'validator' => \Lara\Comment\Validation\DefaultValidator::class,
]

validateWithBag() method

If you have multiple form for comment then you want to display error message You can use validateWithBag() method to validate with bag.

$commentToUpdate = Comment::find(1);

$user = Auth::user();

$comment = CommentService::for($commentToUpdate, $user)
            ->validateWithBag()
            ->update();

so whene validation error occur you may the access the error bag

{{ $errors->{$comment->id . 'PUT'}->first('comment') }}

you can access the name error bag with combination of the commentable id and the method PUT and POST

Redirector

Redirector will redirect to the URL if validation fails. The default redirector is \Lara\Comment\Redirect\RedirectBack. This will redirect back with URL fragment #validation-comment-error. If you wish to change this default behavior you could create your own redirect by extends \Lara\Comment\Redirect\Redirect abstract class and change the redirector value on configuration comment file to your own implementation.

return [
    'redirector' => \Lara\Comment\Redirect\RedirectBack::class
];

Policy

You can create your own policy to authorize the action. To create policy class just run the laravel artisan command. For the complete guide see laravel documentation.

php artisan make:policy CommentPolicy

Don't forget to change policy class in config file.

return [
    'policy' => \Lara\Comment\CommentPolicy::class
]

alayubi/laravel-comment 适用场景与选型建议

alayubi/laravel-comment 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 23 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 06 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 alayubi/laravel-comment 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 alayubi/laravel-comment 我们能提供哪些服务?
定制开发 / 二次开发

基于 alayubi/laravel-comment 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 23
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2022-06-08