laravolt/mural

Laravel评论流

1.0.1 2016-08-04 03:08 UTC

This package is auto-updated.

Last update: 2024-09-05 18:46:32 UTC


README

Travis-CI Build Status Coverage Status

Laravolt Mural旨在提供易于集成到基于Laravel的应用程序中的现成评论功能。

此包仍处于开发阶段,不建议在生产环境中使用。

要求

  • jQuery
  • Semantic-ui或Bootstrap

安装

更新composer.json

对于Laravel 5.2

可以运行以下命令

composer require laravolt/mural

或者将以下声明添加到composer.json文件中

"require": {
    ...
    "laravolt/mural": "^1.0"
},

对于Laravel 5.1

将以下声明添加到composer.json文件中

"require": {
    ...
    "laravolt/mural": "^0.5"
}

服务提供者

Laravolt\Mural\ServiceProvider::class,

外观

'Mural'  => Laravolt\Mural\Facade::class,

迁移

php artisan vendor:publish
php artisan migrate

这将添加新的迁移文件2015_08_17_101000_create_comments_table.php并执行该迁移。将添加名为comments的新表到数据库中。

配置mural.php

default_commentable填充为可评论的模型类声明

'default_commentable' => \App\Post::class,

用法

对于每个可评论的模型,添加以下traitinterface

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Laravolt\Mural\CommentableTrait;
use Laravolt\Mural\Contracts\Commentable;

class Post extends Model implements Commentable
{
    use CommentableTrait;

	public function getCommentableTitleAttribute()
	{
		// TODO: Implement getCommentableTitleAttribute() method.
	}

	public function getCommentablePermalinkAttribute()
	{
		// TODO: Implement getCommentablePermalinkAttribute() method.
	}

}

添加CommentableTrait后,模型Post将自动具有对Laravolt\Mural\CommentmorphMany关系。由于这是一个Eloquent关系,因此可以执行以下操作

// mendapatkan semua komentar
Post::find(1)->comments;

// melakukan paginasi komentar
Post::find(1)->comments()->paginate();

// atau aksi apapun, sama seperti relasi Eloquent biasa
Post::find(1)->comments()->orderBy('created_at', 'desc');

对于指定的评论者,添加Commentator接口。

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravolt\Mural\Contracts\Commentator;

class User extends Authenticatable implements Commentator
{
    ...

    public function getCommentatorNameAttribute()
    {
        // return atribut nama
    }

    public function getCommentatorAvatarAttribute()
    {
        // return atribut link avatar
    }

    public function getCommentatorPermalinkAttribute()
    {
        // return atribut link ke detail user
    }

    public function canModerateComment()
    {
        // return boolean
    }
}

快捷方式

显示评论小部件

要在视图中显示评论小部件,如下所示,通常在大多数博客中都可以看到,请添加以下代码到您的视图中

$post = App\Models\Post::find(1);
{!! Mural::render($post, 'sample-room') !!}

完成,laravolt/mural已经包含了ModelControllerView的现成组件,结果如下所示

您还可以根据特定的room对评论进行分组,因此同一内容可以拥有多个评论组。

{!! Mural::render($post, 'collaborator') !!}
{!! Mural::render($post, 'you-can-put-anything-here') !!}

// readonly, user tidak bisa submit komentar
{!! Mural::render($post, 'room', ['readonly' => true]) !!}

对于外观问题,目前支持的主题是semantic-ui。Bootstrap即将推出(有兴趣的可以提交Pull Request)。

添加评论

Mural::addComment($post, 'komentar lagi', 'collaborator'); // room = collaborator

获取评论

Mural::getComments($post, 'room', []);

事件

配置

<?php

return [
    // semantic-ui or bootstrap
    'skin'                => 'semantic-ui',

    // comment per page
    'per_page'            => 5,

    // whether user enable to vote comment or not
    'vote'                => false,

    // default commentable class (deprecated)
    'default_commentable' => \App\Models\Post::class,

    // default model for user commentator
    'default_commentator' => config('auth.providers.users.model')

    'middleware'   => ['web']
];

测试

由于Orchestral/testbench包的影响,必须从本地vendor运行phpunit,而不是全局的phpunit

vendor/phpunit/phpunit/phpunit

路线图

  • 基本的评论流(已完成)
  • 多房间(已完成)
  • 皮肤:semantic-ui(已完成)
  • 皮肤:bootstrap
  • 评论验证
  • 翻译(已完成)
  • 特定评论的永久链接
  • 如果有新评论,实时更新
  • 事件(已完成)
  • 编辑评论
  • 删除评论(已完成)
  • 报告为垃圾邮件
  • 对评论进行投票(喜欢/不喜欢)(已完成)
  • 按最新或点赞排序评论(已完成)

测试

phpunit