|
| 1 | +Comments module for Yii2 |
| 2 | +======================== |
| 3 | + |
| 4 | +This module provide a comments managing system for Yii2 application. |
| 5 | + |
| 6 | +[](https://packagist.org/packages/yii2mod/yii2-comments) |
| 7 | +[](https://packagist.org/packages/yii2mod/yii2-comments) |
| 8 | +[](https://packagist.org/packages/yii2mod/yii2-comments) |
| 9 | +[](https://travis-ci.org/yii2mod/yii2-comments) |
| 10 | + |
| 11 | +Installation |
| 12 | +------------ |
| 13 | + |
| 14 | +The preferred way to install this extension is through [composer](http://getcomposer.org/download/). |
| 15 | + |
| 16 | +Either run |
| 17 | + |
| 18 | +``` |
| 19 | +php composer.phar require --prefer-dist yii2mod/yii2-comments "*" |
| 20 | +``` |
| 21 | + |
| 22 | +or add |
| 23 | + |
| 24 | +```json |
| 25 | +"yii2mod/yii2-comments": "*" |
| 26 | +``` |
| 27 | + |
| 28 | +to the require section of your composer.json. |
| 29 | + |
| 30 | + |
| 31 | +Configuration |
| 32 | +----------------------- |
| 33 | + |
| 34 | +**Database Migrations** |
| 35 | + |
| 36 | +Before using Comments Widget, we'll also need to prepare the database. |
| 37 | +```php |
| 38 | +php yii migrate --migrationPath=@vendor/yii2mod/yii2-comments/migrations |
| 39 | +``` |
| 40 | + |
| 41 | +**Module setup** |
| 42 | + |
| 43 | +To access the module, you need to add the following code to your application configuration: |
| 44 | +```php |
| 45 | +'modules' => [ |
| 46 | + 'comment' => [ |
| 47 | + 'class' => 'yii2mod\comments\Module' |
| 48 | + ] |
| 49 | +] |
| 50 | +``` |
| 51 | + |
| 52 | +**Comments management section setup** |
| 53 | + |
| 54 | +To access the comments management section, you need to add the following code to your application configuration: |
| 55 | +```php |
| 56 | + 'controllerMap' => [ |
| 57 | + 'comments' => 'yii2mod\comments\controllers\ManageController' |
| 58 | + ] |
| 59 | +``` |
| 60 | +> Basically the above code must be placed in the `admin` module |
| 61 | +
|
| 62 | +You can then access to management section through the following URL: |
| 63 | +http://localhost/path/to/index.php?r=comments/index |
| 64 | + |
| 65 | +> If you have added this controller in the module admin, the URL will look like the following |
| 66 | +> http://localhost/path/to/index.php?r=admin/comments/index |
| 67 | +
|
| 68 | +**Notes:** |
| 69 | +> 1) Delete button visible only for user with `admin` role. |
| 70 | +
|
| 71 | +> 2) When you delete a comment, all nested comments will be marked as `deleted`. |
| 72 | +
|
| 73 | +> 3) For change the any function in the CommentModel you can create your own model and change the property `commentModelClass` in the Comment Module. |
| 74 | +
|
| 75 | +> 4) You can implement your own function `getAvatar` in the `userIdentityClass`. Just create the `getAvatar` method in your User model, and return a image path. |
| 76 | +```php |
| 77 | +public function getAvatar() |
| 78 | +{ |
| 79 | + // return some image path |
| 80 | +} |
| 81 | +``` |
| 82 | +> 5) You can implement your own function `getUsername` in the `userIdentityClass`. Just create the `getUsername` method in your User model. |
| 83 | +```php |
| 84 | +public function getUsername() |
| 85 | +{ |
| 86 | + // your custom code |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +Usage |
| 91 | +------------------- |
| 92 | +**Basic example:** |
| 93 | +```php |
| 94 | +// the model to which are added comments, for example: |
| 95 | +$model = Post::find()->where(['title' => 'some post title'])->one(); |
| 96 | + |
| 97 | +<?php echo \yii2mod\comments\widgets\Comment::widget([ |
| 98 | + 'model' => $model, |
| 99 | + 'relatedTo' => 'User ' . \Yii::$app->user->identity->username . ' commented on the page ' . \yii\helpers\Url::current(), // for example |
| 100 | + 'maxLevel' => 3, // maximum comments level, level starts from 1, null - unlimited level. Defaults to `7` |
| 101 | + 'showDeletedComments' => true // show deleted comments. Defaults to `false`. |
| 102 | +]); ?> |
| 103 | +``` |
| 104 | + |
| 105 | +**You can use your own template for render comments:** |
| 106 | + |
| 107 | + ```php |
| 108 | +<?php echo \yii2mod\comments\widgets\Comment::widget([ |
| 109 | + 'model' => $model, |
| 110 | + 'commentView' => '@app/views/site/comments/index' //path to your template |
| 111 | +]); ?> |
| 112 | + ``` |
| 113 | + |
| 114 | +**Use the following code for multiple widgets on the same page:** |
| 115 | + ```php |
| 116 | +echo \yii2mod\comments\widgets\Comment::widget([ |
| 117 | + 'model' => $model, |
| 118 | + ]); |
| 119 | + |
| 120 | +echo \yii2mod\comments\widgets\Comment::widget([ |
| 121 | + 'model' => $model2, |
| 122 | + 'formId' => 'comment-form2', |
| 123 | + 'pjaxContainerId' => 'unique-pjax-container-id' // also you can set the `pjaxContainerId` |
| 124 | + ]); |
| 125 | + ``` |
| 126 | + |
| 127 | +## Internationalization |
| 128 | + |
| 129 | +All text and messages introduced in this extension are translatable under category 'yii2mod.comments'. |
| 130 | +You may use translations provided within this extension, using following application configuration: |
| 131 | + |
| 132 | +```php |
| 133 | +return [ |
| 134 | + 'components' => [ |
| 135 | + 'i18n' => [ |
| 136 | + 'translations' => [ |
| 137 | + 'yii2mod.comments' => [ |
| 138 | + 'class' => 'yii\i18n\PhpMessageSource', |
| 139 | + 'basePath' => '@yii2mod/comments/messages', |
| 140 | + ], |
| 141 | + // ... |
| 142 | + ], |
| 143 | + ], |
| 144 | + // ... |
| 145 | + ], |
| 146 | + // ... |
| 147 | +]; |
| 148 | +``` |
| 149 | + |
| 150 | + |
| 151 | +#### Example comments |
| 152 | +----- |
| 153 | + |
0 commit comments