technovistalimited / notific
Laravel 简单、静态和最小化通知系统
Requires (Dev)
- laravel/framework: >=5.8
README
Notific 是一个简单且最小的 Laravel 静态通知系统。它根据 user_id 存储通知数据,并为 user_id 获取通知。它不是特别功能丰富。
该软件包 没有 UI,它只是一个基于数据库的缓存数据驱动机制,包含有序方法。您必须自己构建 UI。
许可证: GPL-2.0+
需要: Laravel 5.8
测试到: Laravel 8.x
开发者: Mayeenul Islam,Nazmul Hasan
该软件包仅用于组织的内部使用。如果您从中受益,这就是它在这里的原因。我们可能不会全力支持该软件包。但欢迎提交错误报告。
目录
功能
- 使用迁移创建数据库表
- 将通知存储到数据库中
- 从数据库获取通知
- 使用元数据将任何类型的附加信息存储和检索为通知
- 缓存数据以节省一些宝贵的资源 - 可配置
未提供的功能
- 事件监听器和实时通知(但您可以轻松扩展到这些功能)
安装
步骤 1:下载 & 设置
在您的应用根目录打开命令行并运行
composer require technovistalimited/notific
提供者数组
将以下字符串添加到 config/app.php 中的 providers 数组下
Technovistalimited\Notific\NotificServiceProvider::class,
别名数组
将以下行添加到 config/app.php 中的 aliases 数组下
'Notific' => Technovistalimited\Notific\Facades\Notific::class,
步骤 2:发布必要的文件
首先准备配置和迁移文件;在命令行中输入并按回车键
php artisan vendor:publish --tag=notific
步骤 3:准备表格
在您的应用根目录打开命令行并运行迁移
php artisan migrate
配置
在 config/notific.php 中更改配置。
缓存状态
在 'cache' => ['is_cache'] 下设置是否使用缓存。接受值:true(启用),false(禁用)
默认:true - 启用
缓存时间
在 'cache' => ['cache_time'] 下更改时间。接受值:表示秒的正整数。
默认:10 分钟
API:如何使用
如果正确定义了辅助类,则该软件包易于使用
存储通知
要存储通知,只需将以下方法放置在您想要连接的地方。该方法还将清除用户的缓存以更新通知历史记录。
Notific::notify( $userId, $message, $notificationType, $metaData, $createdBy );
$userID : integer/array
要通知的用户 ID 或用户 ID 数组。$message : string
您想要通知的消息。$notificationType : string : (可选)
如果有其他类型的通知,则指定通知类型。
默认:'notification'$metaData : 整数/字符串/数组 : (可选)
传递任何附加信息的。无论是以整数、字符串还是数组的格式传递。
默认值:空$createdBy : 整数 : (可选)
如果您想追踪谁发出的通知。
默认值:空
获取通知
通过用户ID获取通知。
Notific::getNotifications( $userId, $arguments );
$userID : 整数
获取通知的用户ID。$arguments : 数组 : (可选)
查询参数数组。默认值:array()- 基于默认设置获取通知$read_status : 字符串
通知的读取状态。接受:'all'、'read'、'unread'。
默认值:'all'$order : 字符串
指定通知的升序或降序。接受'ASC'、'DESC'。
默认值:'DESC'$orderby : 字符串
按参数排序检索的帖子。可以传递单个选项。接受来自数据库表的任何有效列名。
默认值:'created_at'$paginate : 布尔值
是否启用分页。
默认值:false- 分页未激活$items_per_page : 整数
获取项目数量。接受任何正整数。
默认值:-1- 获取所有内容
获取通知数量
通过用户ID获取通知的总数。
Notific::getNotificationCount( $userId, $status );
$userID : 整数
获取通知的用户ID。$status : 字符串 : (可选)
通知的读取状态。接受:'all'、'read'、'unread'。
默认值:all- 获取所有通知的数量
将通知标记为 已读
将通知标记为已读,当实际上它们是已读的时候。您可能需要AJAX在飞地标记它们为已读。
Notific::markNotificationRead( $userId, $notificationId );
$userID : 整数
为以下用户ID标记通知为已读。$notificationId : 整数 : (可选)
如果您想将每个通知标记为已读,请传递通知ID
默认值:空 - 标记所有为已读
示例
01. 创建一个通知
以下示例中,ID为21的用户将相应地收到通知
// Notified with a simple message. Notific::notify( 21, 'Your application submitted.' ) ); // Notified with a message and date. Notific::notify( 21, sprintf( 'Your application submitted on %s is approved.', date('d F Y') ) ); // Notified with a different type of notification. Notific::notify( 21, 'Your application submitted.', 'message' ) ); // Notified with some meta data incorporated. Notific::notify( 21, 'Your application is approved. Click to see it.', 'notification', array('link' => 'http://link.to/the/application/' ) ) ); // Notified with someone who (with ID 8) assigned the notification Notific::notify( 21, 'Your application submitted.', 'notification', '', 8 ) ); // Notify multiple users (with ID 21, 4, and 5) at a time Notific::notify( [21, 4, 5], 'An application is submitted. Please check.' );
02. 获取通知
以下示例中,我们正在获取ID为21的用户分配的通知
// Get all the notifications. Notific::getNotifications( 21 ); // Get unread notifications only. Notific::getNotifications( 21, array( 'read_status' => 'unread' ) ); // Get read notifications only. Notific::getNotifications( 21, array( 'read_status' => 'read' ) ); // Get read notifications and maximum 10 of them only. Notific::getNotifications( 21, array( 'read_status' => 'all', 'items_per_page' => 10 ) ); // Get all notifications and paginate them to 50 per page only. Notific::getNotifications( 21, array( 'paginate' => true, 'items_per_page' => 50 ) );
03. 获取通知数量
以下示例中,我们正在获取分配给ID为21的用户的通知数量
// Get all notifications of the user ID 21. Notific::getNotificationCount( 21 ); Notific::getNotificationCount( 21, 'all' ); // Get only the 'unread' notifications of the user ID 21. Notific::getNotificationCount( 21, 'unread' ); // Get only the 'read' notifications of the user ID 21. Notific::getNotificationCount( 21, 'read' );
04. 将通知标记为 已读
// Mark all the notifications as 'read' for the user with ID 21. Notific::markNotificationRead( 21 ); // Mark notification number 56 as 'read' for the user with ID 21. Notific::markNotificationRead( 21, 56 );
05. 可能需要反序列化
从WordPress采用,此函数可以检测是否需要反序列化字符串或序列化,如果是序列化数据。如果您自定义方法,您可能需要将序列化的元数据转换回数组 - 此方法将帮助您。
// Unserialize if serialized. Notific::maybeUnserialize( $string );
贡献
欢迎任何错误报告。我们可能不支持您需要的包。但只要它们符合我们的兴趣,我们肯定会尝试修复错误。
如果您想贡献代码,请随意添加拉取请求。
卸载
- 在整个项目中搜索任何
Notific::声明,从源中删除函数或将其注释掉。否则,在卸载后它们将生成致命错误。 - 打开
config/app.php,从providers和aliases数组中删除...NotificServiceProvider::class这一行。 - 从
config/notific.php中删除配置文件notific.php。 - 从
database/migrations/中删除notific迁移文件。 - 手动删除两个数据库表(因为该包尚未发布):表
notifications和表user_notifications。
现在,打开命令控制台,输入
composer remove technovistalimited/notific
您完成了!
致谢
所有功劳首先要归功于万能的上帝。感谢阿米亚·基肖尔·萨先生,是他让我们两个都能制作我们的第一个Laravel包。感谢卡姆鲁尔·哈桑先生审阅进度并提出了他的想法。还要感谢TechnoVista Limited对这次活动的支持。感谢Notifynder - 一个在Packagist上由Fabrizio提供的Laravel通知包,我们跟随他们学习。感谢WordPress - 一个GPL许可的Web框架,我们很感激地使用了他们的一些代码。