• Stars
    star
    158
  • Rank 237,131 (Top 5 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 9 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Comments module for Yii2

Yii2 Comments Extension


This module provides a comments managing system.

Latest Stable Version Total Downloads License Build Status Scrutinizer Code Quality

Support us

Does your business depend on our contributions? Reach out and support us on Patreon. All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist yii2mod/yii2-comments "*"

or add

"yii2mod/yii2-comments": "*"

to the require section of your composer.json.

Configuration

Database Migrations

Before using Comments Widget, we'll also need to prepare the database.

php yii migrate --migrationPath=@vendor/yii2mod/yii2-comments/migrations

Module setup

To access the module, you need to add the following code to your application configuration:

'modules' => [
    'comment' => [
        'class' => 'yii2mod\comments\Module',
    ],
]

NOTE: Module id must be comment and not otherwise. This is because it is referred somewhere in the code with such name. A PR to fix it to use configured module id is welcome.

Now you can access to management section through the following URL: http://localhost/path/to/index.php?r=comments/index

By default only users with admin role have access to comments management section. But, you can override accessControlConfig property for ManageController.

Notes:

  1. Delete button visible only for users with admin role.
  1. When you delete a comment, all nested comments will be marked as deleted.
  1. You can override default CommentModel class by changing the property commentModelClass in the Comment Module.
  1. You can implement your own methods getAvatar and getUsername in the userIdentityClass. Just create this methods in your User model. For example:
public function getAvatar()
{
    // your custom code
}

public function getUsername()
{
    // your custom code
}

Usage

Basic example:

// the model to which are added comments, for example:
$model = Post::find()->where(['title' => 'some post title'])->one();

<?php echo \yii2mod\comments\widgets\Comment::widget([
    'model' => $model,
]); ?>

You can use your own template for render comments:

<?php echo \yii2mod\comments\widgets\Comment::widget([
  'model' => $model,
  'commentView' => '@app/views/site/comments/index' // path to your template
]); ?>

Use the following code for multiple widgets on the same page:

<?php echo \yii2mod\comments\widgets\Comment::widget([
      'model' => $model,
]); ?>

<?php echo \yii2mod\comments\widgets\Comment::widget([
      'model' => $model2,
      'formId' => 'comment-form2',
      'pjaxContainerId' => 'unique-pjax-container-id'
]); ?>

To enable the pagination for comments list use the following code:

<?php echo \yii2mod\comments\widgets\Comment::widget([
      'model' => $model,
      'dataProviderConfig' => [
          'pagination' => [
              'pageSize' => 10
          ],
      ]
]); ?>

Advanced example:

<?php echo \yii2mod\comments\widgets\Comment::widget([
      'model' => $model,
      'relatedTo' => 'User ' . \Yii::$app->user->identity->username . ' commented on the page ' . \yii\helpers\Url::current(),
      'maxLevel' => 2,
      'dataProviderConfig' => [
          'pagination' => [
              'pageSize' => 10
          ],
      ],
      'listViewConfig' => [
          'emptyText' => Yii::t('app', 'No comments found.'),
      ],
]); ?>

Using Events

You may use the following events:

'modules' => [
    'comment' => [
        'class' => 'yii2mod\comments\Module',
        'controllerMap' => [
            'default' => [
                'class' => 'yii2mod\comments\controllers\DefaultController',
                'on beforeCreate' => function ($event) {
                    $event->getCommentModel();
                    // your custom code
                },
                'on afterCreate' => function ($event) {
                    $event->getCommentModel();
                    // your custom code
                },
                'on beforeDelete' => function ($event) {
                    $event->getCommentModel();
                    // your custom code
                },
                'on afterDelete' => function ($event) {
                    $event->getCommentModel();
                    // your custom code
                },
            ]
        ]
    ]
]

Using Comment Plugin Events

$(document).on('beforeCreate', '#comment-form', function (e) {
    if (!confirm("Everything is correct. Submit?")) {
        return false;
    }
    return true;
});

Available events are:

  • beforeCreate
  • afterCreate
  • beforeDelete
  • afterDelete
  • beforeReply
  • afterReply

Internationalization

All text and messages introduced in this extension are translatable under category 'yii2mod.comments'. You may use translations provided within this extension, using following application configuration:

return [
    'components' => [
        'i18n' => [
            'translations' => [
                'yii2mod.comments' => [
                    'class' => 'yii\i18n\PhpMessageSource',
                    'basePath' => '@yii2mod/comments/messages',
                ],
                // ...
            ],
        ],
        // ...
    ],
    // ...
];

Example comments


Alt text

More Repositories

1

yii2-rbac

RBAC Manager for Yii 2
PHP
143
star
2

yii2-cart

Yii2 shopping cart
PHP
119
star
3

yii2-settings

Persistent settings in Yii2
PHP
102
star
4

yii2-swagger

Swagger Documentation Generator for Yii2 Framework
PHP
63
star
5

yii2-enum

Enumerable helper
PHP
63
star
6

yii2-editable

Editable widget and column for gridview.
PHP
54
star
7

base

Yii2 application template
PHP
53
star
8

yii2-cashier

Yii2 Cashier provides an interface to Stripe's subscription billing services.
PHP
45
star
9

yii2-cms

Simple CMS extension
PHP
44
star
10

yii2-sweet-alert

SweetAlert widget for Yii2 framework
PHP
43
star
11

yii2-array-query

Yii2 component that allows for searching/filtering the elements of an array.
PHP
34
star
12

yii2-ftp

FTP Client for Yii2
PHP
33
star
13

collection

Basic collection library for Yii Framework 2.0
PHP
30
star
14

yii2-behaviors

Collection of useful behaviors for Yii Framework 2.0
PHP
29
star
15

yii2-user

Flexible user registration and authentication module for Yii2.
PHP
27
star
16

yii2-link-preview

LinkPreview widget render page preview
PHP
27
star
17

yii2-image

Provides methods for the dynamic manipulation of images. Various image formats such as JPEG, PNG, and GIF can be resized, cropped, rotated.
PHP
26
star
18

yii2-cron-log

Component for logging cron jobs
PHP
23
star
19

yii2-ion-slider

Easily customizable range slider with skins support.
PHP
22
star
20

yii2-tree

Tree widget based on Fancytree extension
PHP
21
star
21

yii2-selectize

selectize.js wrapper for yii2.
PHP
19
star
22

yii2-google-maps-markers

Google Maps Markers Widget for Yii2
PHP
18
star
23

yii2-star-rating

Star rating widget based on jQuery Raty
PHP
18
star
24

yii2-validators

Collection of useful validators for Yii Framework 2.0
PHP
17
star
25

yii2-scheduling

Scheduling extension for Yii2 framework
PHP
17
star
26

yii2-helpers

Collection of useful helper functions for Yii Framework 2.0
PHP
15
star
27

yii2-timezone

Timezone detector
PHP
15
star
28

yii2-bootstrap-notify

Bootstrap Notify widget for Yii2 framework
PHP
14
star
29

yii2-chosen-select

Select Widget based on Chosen jQuery plugin
PHP
14
star
30

yii2-markdown

Markdown Widget for Yii 2
PHP
13
star
31

yii2-c3-chart

Yii2 wrapper for D3-based reusable chart library
PHP
11
star
32

yii2-bx-slider

bx-slider.js wrapper for yii2.
PHP
11
star
33

yii2-braintree

Yii2 Braintree provides an interface to Braintree subscription billing services.
PHP
9
star
34

yii2-gii-extended

This generator generates enumerable classes or controller and views that implement CRUD (Create, Read, Update, Delete) operations for the specified data model.
PHP
8
star
35

yii2-moderation

A simple Content Moderation System for Yii2
PHP
7
star
36

yii2-disqus

Yii2 disqus comment widget
PHP
6
star
37

yii2-feed

social feeds widget
PHP
6
star
38

yii2-pie

d3pie.js wrapper for yii2
PHP
5
star
39

yii2-toggle-column

Provides a toggle data column and action for Yii Framework 2.0
PHP
3
star