sokil / notification-bundle
通知
Requires
- php: ^5.5 || ^7.0
Requires (Dev)
- phpunit/phpunit: >=3.7.38 <6.0
- satooshi/php-coveralls: >=0.7.1 <2.0
- squizlabs/php_codesniffer: ^2.3
- symfony/framework-bundle: ~2.3|~3.0
- symfony/serializer: ~2.3|~3.0
- symfony/swiftmailer-bundle: ~2.0|~3.0
README
安装
使用 composer 安装依赖
composer.phar require sokil/notification-bundle
将包添加到 AppKernel
<?php class AppKernel extends Kernel { public function registerBundles() { $bundles = array( new Sokil\NotificationBundle\NotificationBundle(), ); } }
在文件 ./app/config/security.yml
中添加角色到角色层次结构
security: role_hierarchy: ROLE_NOTIFICATION_MAIL_PREVIEW: [ROLE_USER] ROLE_NOTIFICATION_SCHEMA_MANAGER: [ROLE_USER]
在 ./app/config/routing.yml
中添加路由
notification: resource: "@NotificationBundle/Resources/config/routing.yml" prefix: /notification
创建消息
消息构建器
首先需要创建消息构建器,它从容器中获取一些依赖关系并构建消息实例。它必须扩展 \Sokil\NotificationBundle\MessageBuilder\AbstractBuilder
类
<?php namespace Acme\Notification\Message; use \Sokil\NotificationBundle\MessageBuilder\AbstractBuilder; class SomeMessageBuilder extends AbstractBuilder { }
此构建器必须在容器中注册,并带有 notification.message_builder
标签
acme.notification.message_builder.some: class: Acme\Notification\Message\SomeMessageBuilder tags: - {name: 'notification.message_builder', messageType: 'someMessage', transport: 'email'}
此服务将构建类型为 someMessages
的消息,用于传输 email
。一条消息可能用于不同的传输。在这种情况下,只需添加另一个标签
acme.notification.message_builder.some: class: Acme\Notification\Message\SomeMessageBuilder tags: - {name: 'notification.message_builder', messageType: 'someMessage', transport: 'email'} - {name: 'notification.message_builder', messageType: 'someMessage', transport: 'sms'}
消息构建器集合
集合包含多个不同的消息。集合用于对消息进行分组。它必须扩展类 Sokil\NotificationBundle\MessageBuilder\BuilderCollection
。要注册新集合,定义新服务
acme.notification.message_builder_collection.some: class: Sokil\NotificationBundle\MessageBuilder\BuilderCollection tags: - {name: 'notification.message_builder_collection', collectionName: 'some'}
已经存在名为 default
的集合,定义为服务 notification.message_builder_collection
。
要将消息构建器添加到集合中,设置构建器的 notification.message_builder
标签的 collectionName
属性
acme.notification.message_builder.some: class: Acme\Notification\Message\SomeMessageBuilder tags: - {name: 'notification.message_builder', messageType: 'someMessage', transport: 'email', collectionName, 'some'} - {name: 'notification.message_builder', messageType: 'someMessage', transport: 'sms'}
如果没有指定 collectionName
,则构建器将注册到 default
集合中。
从集合获取构建器
<?php $someSmsMessageBuilder = $container ->get('acme.notification.message_builder_collection.some') ->getBuilder( 'someMessage', // messageType 'email' // transport );
如果集合的服务未定义,它将为您自动创建,因此您只需在消息构建器的标签中定义 collectionName
,然后通过 ID 从容器中获取集合的服务 notification.message_builder_collection.{SOME_COLLECTION_NAME}
。
通知的架构
可用的传输
配置自定义传输
预览
要启用预览,将路由添加到您的 ./app/config/routing.yml
notification: resource: "@NotificationBundle/Resources/config/routing.yml" prefix: /notification
现在,邮件的预览在路由 /notification/preview
上可用。要访问此路由,您需要具有 ROLE_NOTIFICATION_MAIL_PREVIEW
。
例如,我们有在服务中描述的消息构建器
acme.notification.message_builder.some: class: Acme\Notification\Message\SomeMessageBuilder tags: - {name: 'notification.message_builder', messageType: 'someMessageType', transport: 'someTransport', collectionName, 'someCollection'}
要查看预览,请将以下 URL 在您的浏览器中打开
/notification/preview?messageType=someMessageType&transportName=someTransport&collection=someCollection