jcabanillas/yii2-comments

Yii2 的评论模块

安装: 1

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:yii2-extension

dev-master 2024-06-21 04:38 UTC

This package is auto-updated.

Last update: 2024-09-21 05:10:06 UTC


README

Yii2 Comments 扩展


此模块提供了一个评论管理系统。

Latest Stable Version Total Downloads License Build Status Scrutinizer Code Quality

支持我们

您的业务依赖于我们的贡献吗?请通过 Patreon 联系我们并支持我们。所有承诺都将用于维护工作和开发新功能。

安装

安装此扩展的首选方式是通过 composer

运行以下命令:

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

或添加以下内容到您的 composer.json 的 require 部分。

"jcabanillas/yii2-comments": "*"

配置

数据库迁移

在开始使用 Comments Widget 之前,我们还需要准备数据库。

模块设置

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

要访问模块,您需要将以下代码添加到您的应用程序配置中

注意:模块 ID 必须是 comment 而不是其他。这是因为代码中某处使用了这个名字。欢迎提交一个 PR 来修复它,使其使用配置的模块 ID。

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

现在您可以通过以下 URL 访问管理部分: https:///path/to/index.php?r=comments/index

默认情况下,只有具有 admin 角色的用户可以访问评论管理部分。但,您可以覆盖 ManageController 的 accessControlConfig 属性。

备注

删除按钮仅对具有 admin 角色的用户可见。

  1. 当您删除一条评论时,所有嵌套评论都将被标记为 已删除
  1. 您可以通过在 Comment Module 中更改 commentModelClass 属性来覆盖默认的 CommentModel 类。
  1. 您可以在 userIdentityClass 中实现自己的 getAvatargetUsername 方法。只需在您的 User 模型中创建这些方法即可。例如
  1. 使用方法
public function getAvatar()
{
    // your custom code
}

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

基本示例

您可以为渲染评论使用自己的模板

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

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

在同一页面上使用多个小部件的以下代码

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

要启用评论列表的分页,请使用以下代码

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

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

高级示例

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

使用事件

<?php echo \jcabanillas\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.'),
      ],
]); ?>

使用事件

您可以使用以下事件

'modules' => [
    'comment' => [
        'class' => 'jcabanillas\comments\Module',
        'controllerMap' => [
            'default' => [
                'class' => 'jcabanillas\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
                },
            ]
        ]
    ]
]

使用评论插件事件

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

可用事件包括

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

国际化

本扩展中引入的所有文本和信息都可在 'jcabanillas.comments' 类别下进行翻译。您可以使用以下应用程序配置使用此扩展提供的翻译

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

示例评论

Alt text