cakemanager/ cakephp-notifier
Requires
- php: >=5.4.16
- cakephp/cakephp: 3.3.*
Requires (Dev)
This package is not auto-updated.
Last update: 2019-02-20 20:57:46 UTC
README
此插件允许您将简单的通知系统集成到您的应用程序中。
安装
您可以使用composer将此插件安装到您的CakePHP应用程序中。
将此插件作为composer包安装的推荐方法是
composer require bakkerij/notifier
现在通过以下命令加载插件
bin/cake plugin load -b Bakkerij/Notifier
加载插件后,您需要使用以下命令迁移插件使用的表
bin/cake migrations migrate -p Bakkerij/Notifier
发送通知
模板
在发送任何通知之前,我们需要注册一个模板。以下是如何添加模板的示例
$notificationManager->addTemplate('newBlog', [ 'title' => 'New blog by :username', 'body' => ':username has posted a new blog named :name' ]);
在添加新模板时,您必须添加一个title
和body
。两者都可以包含变量,如:username
和:name
。稍后我们将详细介绍这些变量。
通知
现在我们将能够使用我们的newBlog
模板发送新的通知。
$notificationManager->notify([ 'users' => [1, 2], 'recipientLists' => ['administrators'], 'template' => 'newBlog', 'vars' => [ 'username' => 'Bob Mulder', 'name' => 'My great new blogpost' ] ]);
注意:您还可以通过组件发送通知:
$this->Notifier->notify()
。
使用notify
方法我们发送了新的通知。以下是一些所有属性的列表
users
- 这是一个整数或数组,包含要通知的用户ID。因此,当您想通知用户261和373时,请添加[261, 373]
。recipientLists
- 这是一个字符串或数组,包含收件人列表。稍后您将了解更多关于收件人列表的信息。template
- 您添加的模板,例如newBlog
。vars
- 要使用的变量。在模板newBlog
中,我们使用了变量username
和name
。这些变量可以在此处定义。
收件人列表
要向大型群体发送通知,您可以使用收件人列表。您可以使用以下方式注册它们
$notificationManager->addRecipientList('administrators', [1,2,3,4]);
现在我们已创建了一个名为administrators
的收件人列表。
这可以在发送新的通知时使用
$notificationManager->notify([ 'recipientLists' => ['administrators'], ]);
现在,用户1、2、3和4将收到通知。
检索通知
列表
您可以通过getNotifications
方法轻松检索通知。以下是一些示例
// getting a list of all notifications of the current logged in user $this->Notifier->getNotifications(); // getting a list of all notifications of the user with id 2 $this->Notifier->getNotifications(2); // getting a list of all unread notifications $this->Notifier->allNotificationList(2, true); // getting a list of all read notifications $this->Notifier->allNotificationList(2, false);
计数
获取已读/未读通知的计数可以通过countNotifications
方法完成。以下是一些示例
// getting a number of all notifications of the current logged in user $this->Notifier->countNotifications(); // getting a number of all notifications of the user with id 2 $this->Notifier->countNotifications(2); // getting a number of all unread notifications $this->Notifier->countNotificationList(2, true); // getting a number of all read notifications $this->Notifier->countNotificationList(2, false);
标记为已读
要将通知标记为已读,您可以使用 markAsRead
方法。以下是一些示例
// mark a single notification as read $this->Notifier->markAsRead(500; // mark all notifications of the given user as read $this->Notifier->markAsRead(null, 2);
通知实体
您可以在通知实体中使用以下获取器
title
- 包含变量的生成标题。body
- 包含变量的生成正文。unread
- 如果通知尚未阅读,则为布尔值。read
- 如果通知已阅读,则为布尔值。
示例
// returns true or false $entity->get('unread'); // returns the full output like 'Bob Mulder has posted a new blog named My Great New Post' $entity->get('body');
传递到视图
您可以在视图中这样做,以使用通知列表
$this->set('notifications', $this->Notifier->getNotifications());
通知管理器
NotificationManager
是插件的经理。您可以使用以下方式获取实例
NotificationManager::instance();
NotificationManager
的命名空间为:Bakkerij\Notifier\Utility\NotificationManager
。
管理器具有以下方法可用
notify
addRecipientList
getRecipientList
addTemplate
getTemplate
通知组件
Bakkerij/Notifier.Notifier
组件可以在控制器中使用
public function initialize() { parent::initialize(); $this->loadComponent('Bakkerij/Notifier.Notifier'); }
该组件具有以下可用方法
getNotifications
countNotifications
markAsRead
notify
保持联系
如果您需要一些帮助或对这个插件有想法,请随时在 Gitter 上聊天。
拉取请求总是非常受欢迎!