oxenti / cakephp-notifier
Oxenti是从cakemanager Notifier插件分支出来的CakePHP插件
Requires
- php: >=5.4.16
- cakephp/cakephp: ~3.0
Requires (Dev)
This package is not auto-updated.
Last update: 2024-09-14 18:20:38 UTC
README
安装
您可以使用composer将此插件安装到您的CakePHP应用程序中。
安装composer软件包的推荐方式是
composer require oxenti/cakephp-notifier:dev-master
用法
配置
您需要将以下行添加到您的应用程序的bootstrap.php文件中
Plugin::load('Notifier', ['bootstrap' => true, 'routes' => true]);
// or run the following command:
bin/cake plugin install -b -r Notifier
注意:如果您不使用CakeManager插件,则不需要加载路由。
在加载插件后,您需要使用以下内容迁移插件的表
bin/cake migrations migrate -p Notifier
NotificationManager
NotificationManager是插件的Manager。您可以使用以下方式获取其实例
NotificationManager::instance();
NotificationManager的命名空间如下:Notifier\Utility\NotificationManager。
NotifierComponent
`NotifierComponent`可以用于控制器中创建通知并返回数据,如已读/未读通知和总数(如4条未读通知)。
模板
通知在模板中查看,包括变量。在发送新通知时,您告诉通知使用哪个模板。
如何添加模板的示例
$notificationManager->addTemplate('newBlog', [
'title' => 'New blog by :username',
'body' => ':username has posted a new blog named :name'
]);
在添加新模板时,您必须添加一个title和一个body。两者都可以包含变量,如:username和:name。稍后我们将详细介绍这些变量。
删除模板很简单
$notificationManager->removeTemplate('newBlog');
通知
现在我们将能够使用我们的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。这些变量可以在此处定义。
列表
当然,您希望获取每个用户的通知列表。这里有一些示例
// 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);
// 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);
您可以使用类似以下内容在视图中使用通知列表
$this->set('notifications', $this->Notifier->getNotifications());
收件人列表
要向大型组发送通知,您可以使用收件人列表。您可以使用以下方式注册它们
$notificationManager->addRecipientList('administrators', [1,2,3,4]);
现在我们已经创建了一个名为administrators的收件人列表。
这可以在我们发送新通知时使用
$notificationManager->notify([
'recipientLists' => ['administrators'],
]);
现在,用户1、2、3和4将收到通知。
模型/实体
以下getter可以在您的实体中使用
title- 包含变量的生成标题。body- 包含变量的生成正文。unread- 如果通知尚未阅读,则为布尔值。read- 如果通知已阅读,则为布尔值。
示例
// returns true or false
$entity->get('unread');
// returns the full output 'Bob Mulder has posted a new blog named My Great New Post'
$entity->get('body');
保持联系
如果您需要一些帮助或有关于此插件的建议,请随时在 Gitter 上进行交流。
拉取请求(Pull Requests)总是非常受欢迎!