rmrevin/yii2-comments
Composer 安装命令:
composer require rmrevin/yii2-comments
包简介
Comments module for Yii2
README 文档
README
Code Status
Installation
composer require "rmrevin/yii2-comments:~1.4"
Configuration
In config /protected/config/main.php
<?php return [ // ... 'modules' => [ // ... 'comments' => [ 'class' => 'rmrevin\yii\module\Comments\Module', 'userIdentityClass' => 'app\models\User', 'useRbac' => true, ] ], // ... ];
In your User model (or another model implements the interface IdentityInterface) need to implement the interface "\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 } // ... }
In auth manager add rules (if 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', ]));
Updating database schema
After you downloaded and configured rmrevin/yii2-comments,
the last thing you need to do is updating your database schema by applying the migrations:
In command line:
php yii migrate/up --migrationPath=@vendor/rmrevin/yii2-comments/migrations/
Usage
In view
<?php // ... use rmrevin\yii\module\Comments; echo Comments\widgets\CommentListWidget::widget([ 'entity' => (string) 'photo-15', // type and id ]);
Parameters
### Module parameters
-
userIdentityClass (required, string) The user identity class that Yii2 uses to provide identity information about the users in the App.
-
useRbac (optional, boolean) Default TRUE. Defines if the comment system will use Rbac validation to check the comment permissions when trying to update, delete or add new comments.
-
modelClasses (optional, string[]) Stores the user defined model classes that will be used instead of the default ones in the comment system. Must have a key => classname format. e.g.
'Comment' => '@app\comments\CommentModel'
Widget parameters
-
entity (required, string) The entity that will identify the comments under on section from all the comments in this module.
-
theme (optional, string) In case you want to use a theme in your application you should define here it's location.
-
viewParams (optional, array) Data that will be sent directly into the widget view files. Must have a key => data format. The key will be the variable name in the view. The variable
CommentsDataProviderit's already taken. -
options (optional, array) Default
['class' => 'comments-widget']. Option data array that will be sent into the div holding the comment system in your views. -
pagination (optional, array) Pagination configuration that will be used in the comment panel. Default data:
public $pagination = [ 'pageParam' => 'page', 'pageSizeParam' => 'per-page', 'pageSize' => 20, 'pageSizeLimit' => [1, 50], ];
- sort (optional, array) Type of sorting used to retrieve the comments into the panel. Can be sorted by any of the columns defined in the
commenttable. Default data:
'defaultOrder' => [ 'id' => SORT_ASC, ],
-
showDeleted (optional, boolean) Default
True. Defines if the comments panel will show a message indicating the deleted comments. -
showCreateForm (optional, boolean) Default
True. Will show or hide the form to add a comment in this panel.
Extending the package
### Extending Model files
Depending on which ones you need, you can set the modelMap config property:
// ... 'modules' => [ // ... 'comments' => [ 'class' => 'rmrevin\yii\module\Comments\Module', 'userIdentityClass' => 'app\models\User', 'useRbac' => true, 'modelMap' => [ 'Comment' => '@app\comments\CommentModel' ] ] ], // ...
Attention: keep in mind that if you are changing the Comment model, the new class should always extend the package's original Comment class.
### Attaching behaviors and event handlers
The package allows you to attach behavior or event handler to any model. To do this you can set model map like so:
// ... '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'], ] ] ], // ...
Extending View files
You can extend the view files supplied by this package using the theme component in the config file.
// app/config/web.php 'components' => [ 'view' => [ 'theme' => [ 'pathMap' => [ '@vendor/rmrevin/yii2-comments/widgets/views' => '@app/views/comments', // example: @app/views/comment/comment-form.php ], ], ], ],
### Extending Widgets
To extend the widget code and behavior you only have to extend the widget classes and call them instead of the package's ones.
rmrevin/yii2-comments 适用场景与选型建议
rmrevin/yii2-comments 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 31.67k 次下载、GitHub Stars 达 51, 最近一次更新时间为 2015 年 01 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「comment」 「module」 「yii」 「widget」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 rmrevin/yii2-comments 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rmrevin/yii2-comments 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 rmrevin/yii2-comments 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A simple tool for checking that your PHP classes and methods use PHPDocs (PHP DocBlocks Checker fork).
Yii2 LightBox image galary widget uses Lightbox v2.10.0 by Lokesh Dhakar
This Bundle provides threaded comment functionality for Symfony applications
Priveate for SkeekS CMS
This package provides your visitors the possibility to comment stuff and discuss together.
A simple tool for checking that your PHP classes and methods use docblocks.
统计信息
- 总下载量: 31.67k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 53
- 点击次数: 23
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-01-13