pschocke / laravel-notification-settings
让用户决定他们想要通过哪种方式接收哪些事件的提醒
Requires
- php: ^7.4
- illuminate/support: ~6|~7|~8
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- orchestra/testbench: 5.*|6.*
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2024-09-10 21:59:29 UTC
README
本包试图通过给模型提供为不同联系方式设置独立通知设置的能力,来扩展默认的Laravel通知系统。
$model->saveNotificationSettingFromRequest($request); $model->sendNotification(new OrderReceivedNotification($order); // this model will receive the notification on all channels he has configured
安装
您可以通过Composer安装此包
composer require pschocke/laravel-notification-settings
发布并运行迁移
php artisan vendor:publish --provider="pschocke\NotificationSettings\NotificationSettingsServiceProvider" --tag="migrations" php artisan migrate
发布配置文件
php artisan vendor:publish --provider="pschocke\NotificationSettings\NotificationSettingsServiceProvider" --tag="config"
使用方法
此包基于驱动。默认情况下,它只能发送邮件通知。但是,添加驱动非常简单。
要配置此包,您首先需要发布配置文件。它看起来像这样
<?php return [ /** * The Settings the user can choose if he wants to be notified at a given event */ 'settings' => [ /** * The default model. Add more if you want different settings for different models */ 'default' => [ /** * The default events for this model. Add a new array with the notitication key to have different settings for different notification channels */ 'default' => [ 'event1' => 'description1', 'event2' => 'description2', ], ], ], 'verificationNotification' => pschocke\NotificationSettings\Notifications\NotificationSettingVerificationNotification::class, 'handler' => [ 'mail' => pschocke\NotificationSettings\Handler\MailHandler::class, ], 'driver' => [ 'mail' => [ 'verification' => [ 'enabled' => true, 'method' => 'link', ], ], ], ];
如果您想为不同的模型配置不同的设置,只需在数组中添加一个设置即可
'settings' => [ App\Team::class => [ 'default' => [ 'mySetting' => 'description', ] ] 'default' => [ 'default' => [ 'setting1' => 'description1', 'setting2' => 'description2', ], ], ],
如果您想为模型的不同的通知渠道设置不同的设置,只需添加一个包含通知键和设置的数组
'settings' => [ App\Team::class => [ 'default' => [ 'mySetting' => 'description', ] 'mail' => [ 'mailSetting' => 'mailDescription', ] ] 'default' => [ 'default' => [ 'setting1' => 'description1', 'setting2' => 'description2', ], ], ],
准备您的模型
所有需要通知设置的模型都需要使用 pschocke\NotificationSettings\HasNotificationSettings
特性。
向模型添加新的NotificationSetting
要向模型添加通知渠道,只需在那个模型上调用 saveNotificationSetting()
方法,并传递设置数组
$myModel->saveNotificationSetting([ 'type' => 'mail', // the key of the handler 'meta' => [ // everything configured in the handler as required 'email' => 'test@test.com', ], 'settings' => [ // the settings choices of the user 'onNewLogin' => true, 'onNewLogout' => false, ], ]);
发送通知
要向模型发送通知,只需在该模型上调用 sendNotification()
方法。通知将通过所有设置为true的渠道发送
$myModel->sendNotification(new OrderShippedNotification($notification), 'orderShipped'); // the second parameter is the setting name.
编写自己的通知处理器
仅通过电子邮件发送通知可能就足够了,但有时您还希望通过其他渠道发送通知。幸运的是,添加更多处理器非常简单。只需创建一个新的实现 pschocke\NotificationSettings\Handler\HandlerInterface
并扩展 pschocke\NotificationSettings\Handler
基类的新处理器。
以下是一个发送Slack通知的处理器示例
namspace App\NotificationSettingHandler; use pschocke\NotificationSettings\Models\NotificationSetting; use pschocke\NotificationSettings\Handler\Handler; use pschocke\NotificationSettings\Handler\HandlerInterface; class SlackHandler extends Handler implements HandlerInterface { protected $request = [ 'webhook' => ['required', 'url'], ]; // the request that needs to pass in order to save the model and enabled routing const via = 'slack'; // the channel name that is used in the notifications via method protected $notificationChannel = 'slack'; // the key that is used in the config file public function canSend(string $methodName): bool { return $methodName === 'routeNotificationForSlack'; // the method name to route the notification } public function getSend(NotificationSetting $notificationSetting) // returns what is needed to route the notification { return $notificationSetting->meta['webhook']; } }
添加处理器后,只需将其添加到配置文件中的处理器数组即可
'handler' => [ 'mail' => pschocke\NotificationSettings\Handler\MailHandler::class, 'slack' => App\NotificationSettingHandler\SlackHandler::class, ],
测试
composer test
贡献
有关详细信息,请参阅 CONTRIBUTING
安全
如果您发现任何与安全相关的问题,请通过电子邮件 patrick@ausbildung-ms.de 而不是使用问题跟踪器。
致谢
许可协议
MIT 许可协议 (MIT)。有关更多信息,请参阅 许可文件