Download the PHP package tizis/lara-comments without Composer
On this page you can find all versions of the php package tizis/lara-comments. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package lara-comments
README на русском
laraComments
This package can be used to comment on any model you have in your application.
Features
- [x] View comments
- [x] Create comment
- [x] Delete comment
- [x] Edit comment
- [x] Reply to comment
- [x] Authorization rules | with customization
- [x] View customization
- [x] Dispatch events
- [x] Likes | Dislikes | Comment rating
- [x] API for basic function: get, update, delete, create
- [x] HTML filter customization (using HTMLPurifier)
Upgrade guides →
- From 2.x.x to 3.0
Requirements
- php 7.1 +
- laravel 5.6 +
Your application should contain auth module.
- Laravel 6.x: https://laravel.com/docs/6.x/authentication
- Laravel 5.6: https://laravel.com/docs/5.6/authentication
Installation
1. Run migrations
We need to create the table for comments.
2. Add Commenter trait to your User model
Add the Commenter
trait to your User model so that you can retrieve the comments for a user:
3. Create Comment model
4. Add Commentable
trait and the ICommentable
interface to models
Add the Commentable
trait and the ICommentable
interface to the model for which you want to enable comments for:
5. Custom comment policy (optional)
If you need, you can overwrite default comment policy class:
Then register policy in AuthServiceProvider
:
And add policy prefix to comments.php config
Publish Config & configure (optional)
In the config
file you can specify:
- where is your User model located; the default is
\App\User::class
- where is your Comment model located; the default is
\App\Comment::class
- policy prefix, you can create custom policy class and extends
tizis\laraComments\Policies\CommentPolicy;
- allow tags for html filter
- API prefix
Publish the config file (optional):
Publish views (customization)
The default UI is made for Bootstrap 4
, but you can change it
however you want.
⚠⚠⚠⚠WARNING⚠⚠⚠⚠
All view examples include js/css files for correct working. The possibility of conflict
with your scripts and styles.
Usage
1. Backend rendering:
In the view where you want to display comments, place this code and modify it:
In the example above we are setting argument the model
as class of the book model.
Behind the scenes, the package detects the currently logged in user if any.
If you open the page containing the view where you have placed the above code, you should see a working comments form.
2. Frontend rendering (API):
Title | Method | Url | Params | Route name |
---|---|---|---|---|
Get comments | GET | /api/comments/ | commentable_encrypted_key, order_by (column name, default is id), order_direction (default is asc) | route('comments.get') |
Store comment | POST | /api/comments/ | commentable_encrypted_key, message | route('comments.store') |
Delete comment | DELETE | /api/comments/{comment_id} | -- | route('comments.delete', $comment_id) |
Edit comment | POST | /api/comments/{comment_id} | message | route('comments.update', $comment_id) |
Reply to comment | POST | /api/comments/{comment_id} | message | route('comments.reply', $comment_id) |
Vote to comment | POST | /api/comments/{comment_id}/vote | vote(bool) | route('comments.vote', $comment_id) |
3. Access to the comment service
If you don't want use out of the box features: API, or the CommentController, but want to access the built-in features - you can use tizis\laraComments\UseCases\CommentService
CommentService
class used inside default comment controller for request processing.
To disable API routes by default, set the route.root => null
config value.
Methods:
-
Сreate comment:
CommentService::createComment
- Delete comment:
CommentService::deleteComment
- Delete comment:
- Update comment:
CommentService::updateComment
Events
This package fires events to let you know when things happen.
tizis\laraComments\Events\CommentCreated
tizis\laraComments\Events\CommentUpdated
tizis\laraComments\Events\CommentDeleted
API preprocessing
⚠ WARNING! Only for API! ⚠
Supported preprocessors for attributes of get api:
- user [Object]
- comment [String]
1. Description
Sometimes additional processing of content is necessary before transmission over API.
2. Config:
3. Contract
Create preprocessor class and implement ICommentPreprocessor
interface:
Examples:
Comment:
User:
4. Example:
Without preprocessing:
With preprocessing:
Features of Commentable model
- Scope withCommentsCount()
Example:
Static Helper
use tizis\laraComments\Http\CommentsHelper;
Methods:
- getNewestComments(default $take = 10, default $commentable_type = null)
- getCommenterRating(int $userId, [optional Carbon $cacheTtl])
- moveCommentTo(CommentInterface $comment, ICommentable $newCommentableAssociate)
- moveCommentToAndRemoveParentAssociateOfRoot(CommentInterface $comment, ICommentable $newCommentableAssociate)
Example:
Examples
This repository include only bootstrap4
template, but you can create you own UI. This is just a example of package features.
This is example of backend
rendering, this way have bad performance
when 100+ comments on post due to the need to check user permissions (reply, edit, delete etc) for each comment.
A good idea
is use API and build UI with Vue js (or any other library) with verification of user permissions (only for UI) on frontend.
- Build with bootstrap 4
Upgrade guides
From 2.x.x to 3.0
commentable_type
and commentable_id
request attributes was merged into single
You need to replace these deprecated attributes.
Example:
All versions of lara-comments with dependencies
illuminate/support Version ~5.6.0|~5.7.0|~5.8.0|^6.0|^7.0|^8.0|^9.0|^10.0
illuminate/database Version ~5.6.0|~5.7.0|~5.8.0|^6.0|^7.0|^8.0|^9.0|^10.0
illuminate/http Version ~5.6.0|~5.7.0|~5.8.0|^6.0|^7.0|^8.0|^9.0|^10.0
mews/purifier Version ^2.1|^3.0