rmrevin / yii2-comments
Yii2的评论模块
Requires
- php: >=5.4.0
- rmrevin/yii2-fontawesome: ~2.10
- yiisoft/yii2: 2.0.*
README
代码状态
安装
composer require "rmrevin/yii2-comments:~1.4"
配置
在配置 /protected/config/main.php
<?php return [ // ... 'modules' => [ // ... 'comments' => [ 'class' => 'rmrevin\yii\module\Comments\Module', 'userIdentityClass' => 'app\models\User', 'useRbac' => true, ] ], // ... ];
在你的 User
模型(或实现 IdentityInterface
接口的其他模型)中需要实现接口 "\rmrevin\yii\module\Comments\interfaces\CommentatorInterface"
class User extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface, \rmrevin\yii\module\Comments\interfaces\CommentatorInterface { // ... public function getCommentatorAvatar() { return $this->avatar_url; } public function getCommentatorName() { return $this->name; } public function getCommentatorUrl() { return ['/profile', 'id' => $this->id]; // or false, if user does not have a public page } // ... }
在认证管理器中添加规则(如果 Module::$useRbac = true
)
<?php use \rmrevin\yii\module\Comments\Permission; use \rmrevin\yii\module\Comments\rbac\ItsMyComment; $AuthManager = \Yii::$app->getAuthManager(); $ItsMyCommentRule = new ItsMyComment(); $AuthManager->add($ItsMyCommentRule); $AuthManager->add(new \yii\rbac\Permission([ 'name' => Permission::CREATE, 'description' => 'Can create own comments', ])); $AuthManager->add(new \yii\rbac\Permission([ 'name' => Permission::UPDATE, 'description' => 'Can update all comments', ])); $AuthManager->add(new \yii\rbac\Permission([ 'name' => Permission::UPDATE_OWN, 'ruleName' => $ItsMyCommentRule->name, 'description' => 'Can update own comments', ])); $AuthManager->add(new \yii\rbac\Permission([ 'name' => Permission::DELETE, 'description' => 'Can delete all comments', ])); $AuthManager->add(new \yii\rbac\Permission([ 'name' => Permission::DELETE_OWN, 'ruleName' => $ItsMyCommentRule->name, 'description' => 'Can delete own comments', ]));
更新数据库模式
下载并配置 rmrevin/yii2-comments
后,你需要做的最后一件事是通过应用迁移来更新你的数据库模式
在 命令行
php yii migrate/up --migrationPath=@vendor/rmrevin/yii2-comments/migrations/
用法
在视图中
<?php // ... use rmrevin\yii\module\Comments; echo Comments\widgets\CommentListWidget::widget([ 'entity' => (string) 'photo-15', // type and id ]);
参数
### 模块参数
-
userIdentityClass (必需,字符串) Yii2使用的用户身份类,用于提供关于应用中用户的信息。
-
useRbac (可选,布尔值) 默认 TRUE。定义评论系统是否会使用 Rbac 验证来检查更新、删除或添加新评论时的评论权限。
-
modelClasses (可选,字符串[]) 存储用户定义的模型类,这些类将用于替换评论系统中默认的类。必须具有键 => 类名格式。例如
'Comment' => '@app\comments\CommentModel'
小部件参数
-
entity (必需,字符串) 将标识此模块中所有评论下的某个部分的实体。
-
theme (可选,字符串) 如果你想在应用中使用主题,你应该在这里定义它的位置。
-
viewParams (可选,数组) 将直接发送到小部件视图文件的数据。必须具有键 => 数据格式。键将是视图中的变量名。变量
CommentsDataProvider
已被占用。 -
options (可选,数组) 默认
['class' => 'comments-widget']
。将发送到包含评论系统的 div 中的选项数据数组。 -
pagination (可选,数组) 将在评论面板中使用的分页配置。默认数据
public $pagination = [ 'pageParam' => 'page', 'pageSizeParam' => 'per-page', 'pageSize' => 20, 'pageSizeLimit' => [1, 50], ];
- sort (可选,数组) 用于检索面板中评论的排序类型。可以是
comment
表中定义的任何列进行排序。默认数据
'defaultOrder' => [ 'id' => SORT_ASC, ],
-
showDeleted (可选,布尔值) 默认
True
。定义评论面板是否显示指示已删除评论的消息。 -
showCreateForm (可选,布尔值) 默认
True
。将显示或隐藏在此面板中添加评论的表单。
扩展包
### 扩展模型文件
根据你的需求,你可以设置 modelMap
配置属性
// ... 'modules' => [ // ... 'comments' => [ 'class' => 'rmrevin\yii\module\Comments\Module', 'userIdentityClass' => 'app\models\User', 'useRbac' => true, 'modelMap' => [ 'Comment' => '@app\comments\CommentModel' ] ] ], // ...
注意:请记住,如果你正在更改 Comment
模型,新类应始终扩展包的原始 Comment
类。
### 附加行为和事件处理器
此包允许你将行为或事件处理器附加到任何模型。为此,你可以设置模型映射如下
// ... 'modules' => [ // ... 'comments' => [ 'class' => 'rmrevin\yii\module\Comments\Module', 'userIdentityClass' => 'app\models\User', 'useRbac' => true, 'modelMap' => [ 'Comment' => [ 'class' => '@app\comments\CommentModel', 'on event' => function(){ // code here }, 'as behavior' => ['class' => 'Foo'], ] ] ], // ...
扩展视图文件
你可以通过配置文件中的 theme
组件来使用此包提供的视图文件。
// app/config/web.php 'components' => [ 'view' => [ 'theme' => [ 'pathMap' => [ '@vendor/rmrevin/yii2-comments/widgets/views' => '@app/views/comments', // example: @app/views/comment/comment-form.php ], ], ], ],
### 扩展小部件
要扩展小部件代码和行为,你只需扩展小部件类并调用它们而不是包的小部件即可。