Giter Club home page Giter Club logo

yii2-comments's Introduction

Comments module for Yii2

This module provide a comments managing system for Yii2 application.

Latest Stable Version Total Downloads License Build Status

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'
    ]
]

Comments management section setup

To access the comments management section, you need to add the following code to your application configuration:

  'controllerMap' => [
      'comments' => 'yii2mod\comments\controllers\ManageController'
  ]  

Basically the above code must be placed in the admin module

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

If you have added this controller in the module admin, the URL will look like the following http://localhost/path/to/index.php?r=admin/comments/index

Notes:

  1. Delete button visible only for user with admin role.
  1. When you delete a comment, all nested comments will be marked as deleted.
  1. For change the any function in the CommentModel you can create your own model and change the property commentModelClass in the Comment Module.
  1. You can implement your own function getAvatar in the userIdentityClass. Just create the getAvatar method in your User model, and return a image path.
public function getAvatar()
{
    // return some image path
}
  1. You can implement your own function getUsername in the userIdentityClass. Just create the getUsername method in your User model.
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,
    'relatedTo' => 'User ' . \Yii::$app->user->identity->username . ' commented on the page ' . \yii\helpers\Url::current(), // for example
    'maxLevel' => 3, // maximum comments level, level starts from 1, null - unlimited level. Defaults to `7`
    'showDeletedComments' => true // show deleted comments. Defaults to `false`.
]); ?>

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:

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

echo \yii2mod\comments\widgets\Comment::widget([
      'model' => $model2,
      'formId' => 'comment-form2',
      'pjaxContainerId' => 'unique-pjax-container-id' // also you can set the `pjaxContainerId`
  ]); 

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

yii2-comments's People

Stargazers

 avatar

Watchers

Роман avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.