litemerafrukt/postcomments

litemerafrukt postcomments 模块。

1.1.0 2017-10-15 11:21 UTC

This package is not auto-updated.

Last update: 2024-09-29 04:42:57 UTC


README

Latest Stable Version Build Status CircleCI Build Status Scrutinizer Code Quality Code Coverage

Reddit-like comments to posts.

使用递归视图文件来实现嵌套评论。有默认视图文件。如果您想定义自己的视图,请查看包含的视图文件作为示例。Comments类接受一个顶级视图文件作为可选参数。

CommentsHandler类需要一个简单的具有query-方法的数据库类。如果您想构建自己的数据库(您应该这样做),请查看提供的Database类以获取接口。数据库需要设置一个评论表,请参阅src/extras文件夹中的模式。表名可以可选地提供给CommentsHandler构造函数,默认为r1_comments

此模块旨在适用于Anax项目,但经过一些工作后应适用于任何项目。同时,这不是一个万能包,这是为特定项目编写的。

安装

PHP版本 > 7.0。

$ composer require litemerafrukt/postcomments

使用数据库设置一个用于评论的表,请参阅vendor/litemerafrukt/postcomments/src/extras

使用命名空间为litemerafrukt\Database的数据库类,或提供自己的具有查询方法的类,请参阅vendor/litemerafrukt/postcomments/src/Database/Database.php以获取接口。

使用

设置您的论坛帖子控制器以处理get和post请求。postcomments模块将为视图提供评论的HTML。

来自Anax项目的示例。

class PostController

    /**
     * Show a post
     *
     * @param int $postId
     */
    public function showPost($id)
    {
        $post = $this->posts->fetch($id);

        $user = $this->di->get('user');
        $user->isUser = $user->isLevel(UserLevels::USER);
        $user->isAdmin = $user->isLevel(UserLevels::ADMIN);

        $comments = new Comments(new CommentHandler($this->di->get('olddb')));

        if ($this->di->request->getPost('new-comment-submitted', false) && $user) {
            $authorId = $user->id;
            $authorName = $user->name;
            $parentId = $this->di->request->getPost('parent-id', 0);
            $text = \trim($this->di->request->getPost('comment-text'));
            $comments->new($id, $parentId, $authorId, $authorName, $text);

            $this->di->get("response")->redirectSelf();
        } else if ($this->di->request->getPost('edit-comment-submitted', false) && $user) {
            $id = $this->di->request->getPost('comment-id', null);
            $text = \trim($this->di->request->getPost('comment-text', ''));
            $comments->update($id, $text, function ($comment) use ($user) {
                return $comment['id'] === $user->id;
            });

            $this->di->get("response")->redirectSelf();
        }

        $commentsHTML = $comments->getHtml($id, $user->isUser, $user->isAdmin, $user->name, $user->id);

        $this->renderPage("posts/post", $post->subject, \compact('post', 'user', 'commentsHTML'));
    }

    ..

许可

此软件携带MIT许可证。

 .
..:  Copyright (c) 2017 Anders Nygren (litemerafrukt@gmail.com)