karerylo/ cakephp-comments
一个完全可定制的 CakePHP 3 评论插件。
v0.0.4
2017-05-02 19:09 UTC
Requires
- php: >=5.6.0
- cakephp/cakephp: >=3.4 <4.0.0
- cakephp/migrations: @stable
Requires (Dev)
- cakephp/cakephp-codesniffer: 2.4.*
- phpunit/phpunit: ^5.7
This package is auto-updated.
Last update: 2024-09-26 02:11:40 UTC
README
Comments 插件将允许你在 APP 中更改模板,为每个模型添加评论功能。
此插件与行为和助手一起工作,您需要加载它们才能完全工作。
要求
- CakePHP 3.4+
- PHP 5.6+
- AuthComponent
安装
composer require kareylo/cakephp-comments
在您的 config/bootstrap.php
中加载插件
Plugin::load('Kareylo/Comments', [ 'routes' => true ]);
在您想要可评论的模型表中添加以下行为
$this->addBehavior('Kareylo/Comments.Commentable');
行为可以接受以下选项
- modelClass : ModelTable 的类名。
- 默认值 :
null
- 默认值 :
- commentClass : 如果您有一个 CommentsTable,则为其命名。
- 默认值 :
Kareylo/Comments.Comments
- 默认值 :
- foreignKey : 您自定义的外键名称。
- 默认值 :
ref_id
- 默认值 :
- countComments : 如果您想让模型计数其评论,请设置为 true
- 默认值 :
false
- 默认值 :
- fieldCounter : 您计数字段名称
- 默认值 :
comments_count
- 默认值 :
在您的 src/View/AppView.php
中添加以下助手
public function initialize() { $this->loadHelper('Kareylo/Comments.Comment'); }
助手可以接受以下选项
- type 将围绕您的评论的 HTML 标签
- 默认值
ul
- 接受
ul
、ol
、div
- 默认值
- typeClass 您的类型需要的 CSS 类
- 默认值
null
- 默认值
- subTypeClass 您的子类型需要的 CSS 类
- 默认值
null
- 默认值
- loadJS 如果您想加载默认 JS,请设置为 true
- 默认值
false
- 默认值
用法
通过评论查找器获取所有评论
$data = $this->Model->find()->where(['Model.id' => $id])->find('comments')->first(); $this->set(compact('data'));
显示评论
$this->Comment->display($data);
您也可以选择不使用 display($data)
,而是使用循环来完全控制您的模板
// in your view <div class="row"> <h4>Commentaires</h4> <ul class="comment-list"> <?php foreach ($model->comments as $comment): echo $this->Comment->comment($comment); endforeach; ?> </ul> <!-- loadJS and display the comment Form if user is connected --> <?= $this->Comment->loadFormAndJS($model); ?> </div>
模板
要为评论块(1 个评论)和表单块创建模板,请创建您想要查看的视图,如果 src/Template/Element/Comments
中不存在。例如
/** src/Template/Element/Comments/comment.ctp * $connected is used to check is user is connected */ <div class="comment-avatar"> <i class="fa fa-user"></i> </div> <div class="comment-container"> <div class="comment-author"> <?= $comment->user->username; ?> <span class="comment-date">on <span class="underline"><?= $comment->created->format("l, d M y"); ?></span> at <span class="underline"><?= $comment->created->format("H:i:s"); ?></span></span> </div> <div class="comment-content"> <?= h($comment->id); ?> </div> <?php if ($connected): ?> <div class="comment-btn pull-left"> <a href="#" class="reply" data-id="<?= $comment->id ?>"><i class="fa fa-reply"></i> Reply</a> </div> <?php endif; ?> <?php if ($comment->children): ?> <ul class="comment-list"> <?php foreach ($comment->children as $child) { echo $this->Comment->comment($child); } ?> </ul > <?php endif; ?> </div> // src/Template/Element/Comments/form.ctp <?= $this->Form->create($comment, ['id' => 'commentForm', 'url' => ['controller' => 'Comments', 'action' => 'add', 'plugin' => 'Comments']]); ?> <?= $this->Flash->render('comment'); ?> <?= $this->Form->control('content', ['label' => __('Commentaire'), 'type' => 'textarea']); ?> <?= $this->Form->hidden('ref'); ?> <?= $this->Form->hidden('ref_id'); ?> <?= $this->Form->unlockField('parent_id'); ?> <?= $this->Form->hidden('parent_id', ['default' => null]); ?> <?= $this->Form->button(__('Commenter')) ?> <?= $this->Form->end() ?>
要为您的闪光灯创建模板,只需在 src/Template/Element/Flash/Comments
中添加一个元素,并命名为 comment.ctp
//src/Template/Element/Flash/Comments/comment.ctp <?php if (!isset($params['escape']) || $params['escape'] !== false) { $message = h($message); } ?> <div class="message <?= $params['class'] ?? 'success' ?>" onclick="this.classList.add('hidden');"><?= $message ?></div>
$params['class']
可以有success
和error
的值
支持
有关错误和功能请求,请使用此存储库的 问题 部分。
贡献
许可
在 MIT 许可下发布。必须保留包含在此存储库中的源代码中每个文件的版权声明。
待办事项
- 测试用例
- 改进测试用例(如测试助手)
- 更多功能
- 翻译