cakeplugins / notifier
Requires
- php: >=5.4.16
- cakephp/cakephp: 3.3.*
Requires (Dev)
This package is not auto-updated.
Last update: 2019-02-20 18:58:35 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 上聊天。
拉取请求总是非常受欢迎!