This module allows you to easy integrate comments system into your Yii2 application.
- Either run
composer require --prefer-dist totojack/yii2-comments "*"
or add
"totojack/yii2-comments": "*"
to the require section of your composer.json
file.
- Run migrations
yii migrate --migrationPath=@vendor/totojack/yii2-comments/migrations/
- In your config file
'bootstrap' => ['comments'],
'modules'=>[
'comments' => [
'class' => 'yeesoft\comments\Comments',
],
],
- In you model [optional]
public function behaviors()
{
return [
'comments' => [
'class' => 'yeesoft\comments\behaviors\CommentsBehavior'
]
];
}
- Widget namespace
use yeesoft\comments\widgets\Comments;
- Add comment widget in model view using (string) page key :
echo Comments::widget(['model' => $pageKey]);
- Or display comments using model name and id:
echo Comments::widget(['model' => 'post', 'model_id' => 1]);
- Or display comments using model behavior:
echo Post::findOne(10)->displayComments();
Use this options to configurate comments module:
-
userModel
- User model class name. -
maxNestedLevel
- Maximum allowed nested level for comment's replies. -
onlyRegistered
- Indicates whether not registered users can leave a comment. -
orderDirection
- Comments order direction. -
nestedOrderDirection
- Replies order direction. -
userAvatar
- The field for displaying user avatars.Is this field is NULL default avatar image will be displayed. Also it can specify path to image or use callable type.
If this property is specified as a callback, it should have the following signature:
function ($user_id)
Example of module settings:
'comments' => [ 'class' => 'yeesoft\comments\Comments', 'userAvatar' => function($user_id){ return User::getUserAvatarByID($user_id); } ]