Notifly 允许聚合通知参与者,如 Facebook、Twitter、Instagram 等 -- (John Doe、Jane Doe 和 8 位其他人对您的照片进行了反应。)通知由一个参与者、一个动词、一个对象和一个目标组成。它讲述了一个人在对象上或与对象执行动作的故事。
Requires
- php: ^7.2.5
- league/fractal: ^0.19.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- orchestra/testbench: ^5.0
- phpunit/phpunit: ^8.5
- psalm/plugin-laravel: ^1.2
- vimeo/psalm: ^3.11
This package is auto-updated.
Last update: 2023-03-28 22:20:11 UTC
README
Notifly 允许聚合通知参与者,如 Facebook、Twitter、Instagram 等 -- (John Doe、Jane Doe 和 8 位其他人对您的照片进行了反应。
)通知由一个 参与者
、一个 动词
、一个 对象
和一个 目标
组成。它讲述了一个人在对象上或与对象执行动作的故事。
安装
您可以通过 composer 安装此包
composer require piscibus/notifly
您可以使用以下命令发布和运行迁移
php artisan vendor:publish --provider="Piscibus\Notifly\NotiflyServiceProvider" --tag="migrations" php artisan migrate
您可以使用以下命令发布配置文件
php artisan vendor:publish --provider="Piscibus\Notifly\NotiflyServiceProvider" --tag="config"
这是已发布的配置文件的内容
return [ 'max_actors_count' => 2, 'icons' => [ // 'verb' => VerbIcon::class ], ];
创建通知
在 Notifly 中,与 Laravel 一样,每个通知都由一个单独的类表示(通常存储在 app/Notifications
目录中)。您可以通过运行 notifly:make:notification
Artisan 命令创建通知类。
php artisan notifly:make:notification CommentNotification
此命令将在您的 app/Notifications
目录中放置一个新的通知类。每个通知类都包含一个 via
方法,该方法返回一个支持通道的数组。默认返回 NotiflyChannel
,您可以将任何所需的通道附加到它。NotiflyChannel
替代 Laravel 的数据库通道,但任何其他消息构建通道,如 toMail
,都支持如预期的那样在正常的通知类中。
发送通知
通知用户模型必须实现 TransformableInterface
,不用担心所需的函数,它们在 Piscibus 中的 Notifiable
中实现,而不是在 Laravel 中。让我们看看一个 User
模型示例。
<?php namespace App; use Illuminate\Foundation\Auth\User as Authenticatable; use Piscibus\Notifly\Contracts\TransformableInterface; use Piscibus\Notifly\Traits\Notifiable; class User extends Authenticatable implements TransformableInterface { use Notifiable; }
此特性包含一个 notify
方法,该方法期望接收一个通知实例。
use App\Notifications\CommentNotification; $user->notify(new CommentNotification($actor, $object, $target));
在上面的例子中,$actor
是评论 $user
的帖子的用户。$object
是评论本身。最后,$target
是 $actor
评论的帖子。
所有这些必需的实体($actor
、$object
和 $target
)都必须实现 TransformableInterface
,再次不用担心所需的函数,它们在 Notifly
特性中实现。
<?php use Illuminate\Database\Eloquent\Model; use Piscibus\Notifly\Contracts\TransformableInterface; use Piscibus\Notifly\Traits\Notifly; class Comment extends Model implements TransformableInterface { use Notifly; }
由于 User
模型可能同时代表一个参与者和一个可通知的实体,所需的函数由 Notifibale
特性提供。
tl;dr
- 用于通知的所有模型应实现
\Piscibus\Notifly\Contracts\TransformableInterface
- 通知模型 -- (
User
)使用\Piscibus\Notifly\Traits\Notifiable
特性。 - 任何其他模型 -- (
Comment
、Post
)使用\Piscibus\Notifly\Traits\Notifly
特性。
格式化通知
通知类包含一个 $verb
属性,用描述性词汇替换其值。让我们看看一个 CommentNotification
类。
<?php use Piscibus\Notifly\Notifications\Notification; class CommentNotification extends Notification { /** * @var string */ protected $verb = 'comment'; }
通知通常包含一个图标,图标在JSON响应中用数组表示。要自定义通知图标,需要创建一个图标类,并在配置文件 configs/notifly.php
中注册此图标类。要生成图标类,请运行 notifly:make:icon
Artisan 命令。
php artisan notifly:make:icon CommentNotificationIcon
此命令将在 app/Icons
目录中放置一个新的图标类。
<?php namespace App\Icons; use Piscibus\Notifly\Notifications\Icon; class CommentNotificationIcon extends Icon { /** * Get notification icon attributes * * @return array */ public function toArray(): array { return [ 'width' => 0, 'height' => 0, 'uri' => null, ]; } }
//configs/notifly.php return [ // .. 'icons' => [ 'comment' => CommentNotificationIcon::class, ] // ];
在 toArray
方法中,您可以访问通知的所有实体。
$this->actors
是通知参与者的集合。$this->object
是通知对象。$this->target
是通知目标。
使用它们来自定义通知图标。
访问通知
一旦通知存储在数据库中,您需要一种方便的方式从可通知实体中访问它们。 \Piscibus\Notifly\Traits\Notifiable
包含一个 notifications
Eloquent 关系,它返回实体的“未读”通知。要获取通知,您可以像访问任何其他 Eloquent 关系一样访问此方法。默认情况下,通知将按 updated_at
时间戳排序。
$user = App\User::find(1); foreach ($user->notifications as $notification){ echo $notification->verb; }
如果您想检索“未查看”的通知,可以使用 unseenNotifications
关系。同样,这些通知将按 updated_at
时间戳排序。
$user = App\User::find(1); $bellCount = $user->unseenNotifications()->count(); foreach ($user->unseenNotifications as $notification){ echo $notification->verb; }
如果您想检索“已读”的通知,可以使用“readNotifications”关系。同样,这些通知将按 updated_at
时间戳排序。
$user = App\User::find(1); foreach ($user->unreadNotifications as $notification) { echo $notification->type; }
将通知标记为已查看
通常,当用户检索通知列表时,您会想将通知标记为“已查看”。 \Piscibus\Notifly\Traits\Notifiable
提供了一个 markAsSeen
方法,它更新通知数据库记录上的 seen_at
列
$user = App\User::find(1); foreach($user->unseenNotifications as $notificaion) { $notification->markAsSeen(); }
但是,您可以直接在通知集合上使用 markAsSeen
方法,而不是遍历每个通知。
$user->unseenNotifications->markAsSeen();
您还可以使用批量更新查询来将所有通知标记为已查看,而无需从数据库中检索它们
$user = App\User::find(1); $user->unseenNotifications()->update(['seen_at' => now()]);
您还可以 delete
通知,将它们从表中完全删除
$user->notifications()->delete();
将通知标记为已读
通常,当用户查看通知时,您会想将通知标记为“已读”。您可以使用 markAsRead
方法,该方法删除此通知条目并在 read_notification
表中创建一个新条目
$user = App\User::find(1); foreach($user->notifications as $notification) { $readNotification = $notification->markAsRead(); }
如果您想将已读通知标记为“未读”,可以使用 markAsUnRead
方法,该方法逆转了标记通知为已读的前一个过程。
$user = App\User::find(1); foreach($user->readNotifications as $notification) { $notification->markAsUnRead(); }
通知 JSON 响应
通常,您希望提供可通知通知的 JSON 响应。您可以使用 \Piscibus\Notifly\Resources\JsonNotifications
JSON 资源。为了避免“N+1”查询,\Piscibus\Notifly\Traits\Notifiable
有一个 jsonableNotifications
和 jsonableReadNotifications
关系,它们会预先加载所需的关联
Route::get('/notifications', function(){ $user = App\User::find(1); $notifications = $user->jsonableNotifications; return JsonNotifications::collection($notifications); })
通知 JSON 如下所示
{ "id": "9122a3cb-b4e3-352a-a1ab-087e259403af", "verb": "comment", "time": "2020-07-26T16:07:10.000000Z", "object" : { "id" : 7, //... }, "target": { "id": 9 }, "actors": { "data": [ { "id": 1990, //... }, { "id": 1995, //... } ], "trimmed": 8 }, "icon": { "width": 500, "height": 500, "uri": "https://example.com/path/to/image.jpg" } }
自定义通知实体 JSON
要自定义通知 JSON,创建一个 Eloquent API Resource,然后在实体模型类中重写 getTransformer
。在 John commented on your post
的情况下,对象是一个 Comment
模型,要自定义其 JSON
php artisan make:resource Comment
<?php namespace App; use Illuminate\Database\Eloquent\Model; use Piscibus\Notifly\Contracts\TransformableInterface; use Piscibus\Notifly\Traits\Notifiable; use App\Http\Resources\Comment as CommentResource; class Comment extends Model implements TransformableInterface { use Notifiable; /** * Gets the Eloquent API Resource **/ public function getTransformer(): JsonResource { return new CommentResource($this); } }
有关Eloquent API Resource的更多信息,请查阅Laravel文档。
Artisan命令
创建通知
notifly:make:notification
- 描述:创建一个新的通知类。
- 用法:
notifly:make:notification [options] [--] <name> [<verb>]
- 参数:
name
*:(必填) 通知类的名称。verb
:(可选) 通知动词的名称。
- 选项:
-i
,--icon
:为创建的通知类创建图标类。
创建图标
notifly:make:icon
- 描述:创建一个新的通知图标类。
- 用法:
notifly:make:icon <name>
- 参数:
name
*:(必填) 通知图标类的名称。