dariob/cakephp-notifications

该包最新版本(1.0.0)没有提供许可信息。

CakePHP 3.8.x 的通知插件

1.0.0 2020-02-04 11:13 UTC

This package is not auto-updated.

Last update: 2024-09-22 20:38:09 UTC


README

Travis Packagist Packagist Gitter

此插件允许您将简单的通知系统集成到您的应用程序中。

安装

您可以使用 composer 将此插件安装到您的 CakePHP 应用程序中。

安装此插件的推荐方法是通过 composer 包

    composer require dariob/cakephp-notifications

现在通过以下命令加载插件

    bin/cake plugin load -b dariob/cakephp-notifications

加载插件后,您需要使用以下命令迁移插件表

    bin/cake migrations migrate -p dariob/cakephp-notifications

发送通知

模板

在发送任何通知之前,我们需要注册一个模板。以下是如何添加模板的示例

    $notificationManager->addTemplate('newBlog', [
        'title' => 'New blog by :username',
        'body' => ':username has posted a new blog named :name'
    ]);

添加新模板时,您必须添加 titlebody。两者都可以包含如 :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 模板中,我们使用了变量 usernamename。这些变量可以在这里定义。

接收者列表

要向大型组发送通知,您可以使用接收者列表。您可以使用以下方式注册它们

    $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 是该插件的 Manager。您可以使用以下方式获取其实例

    NotificationManager::instance();

NotificationManager 的命名空间为 Bakkerij\Notifier\Utility\NotificationManager

Manager 具有以下可用方法

  • notify
  • addRecipientList
  • getRecipientList
  • addTemplate
  • getTemplate

通知组件

您可以在控制器中使用 Bakkerij/Notifier.Notifier 组件

    public function initialize()
    {
        parent::initialize();
        $this->loadComponent('Bakkerij/Notifier.Notifier');
    }

组件具有以下可用方法

  • getNotifications
  • countNotifications
  • markAsRead
  • notify

保持联系

如果您需要一些帮助或者对这个插件有想法,请随时在 Gitter 上聊天。

欢迎提交Pull Requests!