hamdallahjodah / laravel-notification-settings
让用户决定他们希望通过哪些方式接收哪些事件的提醒
Requires
- php: ^7.4|^8.0|^8.1
- illuminate/support: ~6|~7|~8|^9.0|^10.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- orchestra/testbench: 5.*|6.*
- phpunit/phpunit: ^9.0
This package is not auto-updated.
Last update: 2024-10-03 00:40:23 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']; } }
添加处理器后,只需将其添加到配置文件中的 handlers 数组中
'handler' => [ 'mail' => pschocke\NotificationSettings\Handler\MailHandler::class, 'slack' => App\NotificationSettingHandler\SlackHandler::class, ],
测试
composer test
贡献
有关详细信息,请参阅 CONTRIBUTING
安全
如果您发现任何安全相关的问题,请通过电子邮件发送至 patrick@ausbildung-ms.de,而不是使用问题跟踪器。
鸣谢
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件