mkebza/notificator-bundle

Symfony 组件,用于通过不同的处理器向用户发送通知

安装: 132

依赖者: 0

建议者: 0

安全性: 0

星级: 0

关注者: 1

分支: 0

开放问题: 0

类型:symfony-bundle

v1.1.0 2018-12-31 05:29 UTC

This package is auto-updated.

Last update: 2024-09-29 05:21:16 UTC


README

Build Status

这是一个简单的组件,用于通过不同的渠道管理通知。

安装

使用 Symfony Flex 的应用程序

打开命令行,进入您的项目目录,并执行以下命令

$ composer require mkebza/notificator-bundle

不使用 Symfony Flex 的应用程序

步骤 1:下载组件

打开命令行,进入您的项目目录,并执行以下命令以下载此组件的最新稳定版本

$ composer require mkebza/notificator-bundle

此命令要求您全局安装 Composer,如 Composer 文档中的安装章节所述。

步骤 2:启用组件

然后,通过将其添加到项目 app/AppKernel.php 文件中注册的组件列表中来启用组件

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new MKebza\Notificator\MKebzaNotificatorBundle(),
        );

        // ...
    }

    // ...
}

配置

您至少需要定义一个处理器,否则您的消息将不会发送。对于处理器,必须定义服务处理器。

m_kebza_notificator:
    handlers:
        email:
            service: MKebza\Notificator\Handler\Type\SwiftMailerHandler
            options:
                from_name: Foo Bar
                from_email: foo@bar.com
        email_second:
            service: MKebza\Notificator\Handler\Type\SwiftMailerHandler
        sms: MKebza\Notificator\Handler\Type\SwiftMailerHandler

发送通知

首先,您必须实现您的通知类,该类必须实现 NotificationInterface,并且服务必须标记为 mkebza_notificator.notification

namespace App\Notification;

class TestNotification implements NotificationInterface
{
    public function getChannels(NotifiableInterface $target): array
    {
        // Returns which channels this notification implements
        return [
            'email'
        ];
    }

    // Renders notification for handler
    public function email(Notification $notification, array $options): Notification
    {
        $message = (new \Swift_Message())
            ->setSubject('Test subject')
            ->setBody('Test');

        return $notification->setContent($message);
    }
}

然后您可以发送您的通知。可通知对象的参数是每个渠道的配置。

// $notificator is instance of service MKebza\Notificator\Notificator
$notificator->send(new Notifiable(['email' => 'foo@bar.com']), TestNotification::class, ['data' => 'will_be_passed']);

您可以让您的对象实现 MKebza\Notificator\NotifiableInterface,然后可以调用

$notificator->send($user, TestNotification::class, ['data' => 'will_be_passed']);
$notificator->send([$user1, $user2], TestNotification::class, ['data' => 'will_be_passed']);

处理器

可以实现新的处理器,处理器必须实现 MKebza\Notificator\Handler\NotificationHandlerInterface