hadesarchitect/notification-bundle

为 Symfony 2 环境提供的基于事件的通告组件。

v1.0.1 2015-02-09 08:51 UTC

This package is not auto-updated.

Last update: 2024-09-25 14:28:25 UTC


README

Build Status Latest Stable Version SensioLabsInsight

为 Symfony 2 环境提供的基于事件的通告组件。提供方便的方式来管理应用中的用户通告,例如警告管理员关于重要事件(如用户注册)或通知用户任何对他们来说重要的事情,例如“您的订单已发货”。目前仅支持 swiftmailer 作为通告渠道,但可以轻松扩展。视觉部分可以由任何支持的模板引擎(如 twig)渲染。

安装

通过 composer 安装

第一步:要求组件

composer require hadesarchitect/notification-bundle ~1

第二步:启用组件

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new HadesArchitect\NotificationBundle\HadesArchitectNotificationBundle(),
    );
}

使用方法

示例

我想在有人评论我的帖子时收到通知,假设我已经使用了 FOSCommentBundle 进行评论功能。为此,我需要执行两个简单的步骤

  • 添加一个通告模板
  • 在 app/config/config.yml 中配置处理器

首先,我需要编写一个将被渲染并发送给我的模板。我将使用 twig 作为模板引擎。每个模板将自动从处理器接收一些参数,目前是一个事件、事件名称和接收者信息。让我向您介绍一个小巧的 twig 模板。

{# AcmeBundle:Notification:post_commented.html.twig #}
<h1>Post commented!</h1>
<p>Author: {{ event.comment.authorName }}</p>
<p>Time: {{ event.comment.createdAt|date('H:i:s d/m/Y') }}</p>
<p>Text: {{ event.comment.body }}</p>

之后,我应该配置事件处理器。

ha_notification:
    swiftmailer_channel:
        sender: no-reply@my-project.com
    handlers: 
        post_commented:
            event:         fos_comment.comment.post_persist
            subject:       Post commented! 
            receiver:      administrator@my-project.com
            template:      AcmeBundle:Notification:post_commented.html.twig

就是这样!不要忘记确保 swiftmailer 配置,因为邮件发送可能在开发环境中未配置或禁用。

配置

在初始设置之后,您应该以适当的方式配置组件。通常这可以通过在 app/config/config.yml 中进行一些更改来实现,或者如果您想将用户通告保存在单独的文件中,您应该创建一个新文件,例如 app/config/notifications.yml,并将其包含在 config.yml 中(导入:[{资源:notifications.yml}])。

####典型配置 典型的方法是指定处理器的某些数据:期望的事件、接收者的数据、主题和模板

ha_notification:
    swiftmailer_channel:
        sender: no-reply@my-project.com # We need to specify sender name for swiftmailer
    handlers: # Array of handlers, you could configure as much handlers as you want to. 
        user_registered: # handler name, should be unique
            event:    fos_user.registration.completed # Expected event
            receiver: administrator@mail.com          # Admin email, could be an array of emails
            subject:  New user registered!            # Subject, optional
            template: Acme:Notifications:user_registered.html.twig # Template, optional (but recommended to create custom templates)

####完整配置

ha_notification:
    default_channel: @ha_notification.channel.swiftmailer # Specify default notification channel, swiftmailer by default
    swiftmailer_channel:
        sender: no-reply@my-project.com
    handlers: 
        user_registered:
            event:         fos_user.registration.completed
            subject:       New user registered! 
            receiver:      administrator@mail.com
            template:      Acme:Notifications:user_registered.html.twig
            handler_class: %ha_notification.handler.default_class% # Overrides handler class
            templating:    @templating #Templating engine service # Overrides templating engine for the handler
            channel:       @ha_notification.channel # Overrides notification channel for the handler

参数

您可以覆盖的参数

  • ha_notification.default_subject - 通告的主题
  • ha_notification.view.default_template - 信件主体的模板
  • ha_notification.handler.default_class - 处理器类
  • ha_notification.channel.swiftmailer_class - swiftmailer 通告渠道的默认类

待办事项

  • 描述扩展组件的方法
  • 添加更多测试
  • 添加动态确定接收者地址的可能性
  • 添加直接抛出通告的可能性,而不仅通过事件处理器
  • 添加绕过任何服务和参数到模板的可能性,而不是使用 twig 全局变量