singlephon / hotification
易于配置的事件驱动 Laravel 通知包。
Requires
- php: ^8.2
- illuminate/collections: ^10.0 || ^11.0
- illuminate/console: ^10.0 || ^11.0
- illuminate/database: ^10.0 || ^11.0
- illuminate/log: ^10.0 || ^11.0
- illuminate/notifications: ^10.0 || ^11.0
- illuminate/support: ^10.0 || ^11.0
Requires (Dev)
- phpunit/phpunit: ^10.0
README
Hotification
是一个强大且灵活的用于管理 Laravel 应用通知的包。它提供了一个便捷的 API 来发送通知、添加观察者和处理事件。
主要特性
- 通过单个文件
config/hotification.php
实现易于配置。 - 通过自定义发送者和观察者类进行扩展。
- 立即将通知发送到数据库。
- 动态为模型添加观察者。
- 在模型更改时执行任何逻辑。
安装
- 通过 Composer 安装
composer require singlephon/hotification
php artisan hotification:install
- 注册服务提供者,别名
将提供者,别名添加到 config/app.php
'providers' => [ # ... Singlephon\Hotification\HotificationServiceProvider::class, ], 'aliases' => [ # ... 'Hotification' => \Singlephon\Hotification\Hotification::class, ]
快速入门
发送通知
使用 HotificationSender
类立即将通知发送到数据库
use App\Models\User; (new Hotification()) ->notify(User::find(1)) ->icon('exclamation-triangle') ->title('Important notification...') ->description('Some description...') ->url('/app/friends/invitations') ->actionable() ->send();
这将向 notifications
表添加一条记录。
自动化发送通知
在 Hotification
中,您可以使用模型观察者自动执行特定事件(如创建、更新或删除模型实例)的操作。这允许轻松地将通知和回调集成到标准模型工作流程中,为管理应用行为提供了一种便捷的方式。
config/hotification.php
中的关键设置
'models' => [ # Models for which observers need to be registered ], 'scheduled_notifications' => [ # Settings for scheduled notifications ]
为模型使用观察者
以下示例展示了如何使用钩子如 onCreated
、onUpdated
和 onDeleted
配置 Post
和 User
模型的观察者
'models' => [ \App\Models\Post::class => [ # Send a notification when a new post is created 'onCreated' => [ \App\Notifications\PostCreatedNotification::class, # Should extend Singlephon\Hotification\Notifications\AbstractHotification function (Post $post) { # Callback to execute custom logic when a post is created # For example, sending a Push notification, logging, or modifying other properties }, ], # Send a notification when a post is updated 'onUpdated' => [ \App\Notifications\PostUpdatedNotification::class, ] ], \App\Models\User::class => [ 'onCreated' => [ function (User $user) { # Callback for executing logic when a new user is created # For example, sending a welcome message or logging activity (new Hotification()) ->notify($user) ->icon('check-badge') ->title('Welcome ' . $user->name) ->description('Please continue registration form...') ->url('/app/registration/continue') ->actionable() ->send(); }, ], # Send a notification when a user is deleted 'onDeleted' => [ \App\Notifications\ByeByeNotification::class, function (User $user) { # Callback for executing logic when a user is deleted # For example, sending a notification to the administrator } ] ] ]
⚠️ 注意:通知类必须继承自 Singlephon\Hotification\Notifications\AbstractHotification
类。
使用观察者的优势
- 集中逻辑:允许将所有与模型事件相关的逻辑集中在同一个地方。
- 易于管理:通过简单更新观察者配置,可以轻松地添加或更改事件上的行为。
- 可扩展性:使用内置通知和自定义回调,为事件处理提供灵活性。
建议
- 仅在必要时使用钩子 以避免逻辑中的不必要的复杂性。
- 避免过载单个钩子 以过多的操作来维持性能和易于调试。
- 对于复杂的逻辑,考虑将其移动到单独的服务或类中,以保持代码的清晰和可读性。
计划通知
config/hotification.php
中的 scheduled_notifications
部分,允许您定义应该按计划执行的任务。这些任务可以包括发送通知和使用回调执行自定义逻辑。此功能提供灵活性和方便性,以管理计划任务,简化常规操作的自动化。
'scheduled_notifications' => [ 'weekly_report' => function (Schedule $schedule) { $schedule->call(function () { (new Hotification()) ->notify(User::all()) ->icon('check-badge') ->title('Welcome ' . $user->name) ->description('Please continue registration form...') ->url('/app/registration/continue') ->actionable() ->send(); })->everyWeek(); # Runs every week }, ], 'daily_report' => [ 'events' => [ \App\Notifications\DailyReportNotification::class, # Sending a notification function () { # Logic for executing a custom action # For example, generating a report and sending it via email }, ], 'schedule' => '0 0 * * *', # Every day at midnight ],
⚠️ 注意:通知类必须继承自 Singlephon\Hotification\Notifications\AbstractHotification
类。
使用 scheduled_notifications
的优势
- 灵活性:将通知和自定义回调组合在单个任务中。
- 方便性:通过配置文件轻松添加和修改任务。
- 自动化:自动执行常规任务,如发送报告或执行定期更新。
建议
- 测试您的任务:确保计划正确设置且任务在预期时间运行。
- 避免过于频繁的任务:在合理的间隔内安排任务,以避免系统过载。
- 使用回调处理复杂逻辑:如果任务需要复杂的处理,将逻辑移动到回调或单独的服务中。
运行时动态添加观察者
使用Hotification的关键方面之一是能够在运行时动态地向模型添加观察者(处理程序),而不仅仅是通过配置文件。这提供了高度的灵活性,并允许根据上下文或业务逻辑以编程方式管理通知行为。
示例代码
考虑以下示例,它动态地添加了对User
模型的观察者
(new Hotification()) ->manager() ->add(User::class, onUpdated: [function (User $user) {}], onCreated: [function (User $user) {}], onDeleted: [function (User $user) {}] );
文档也提供俄语版本,请参阅 俄语版本
更新日志
有关最近更改的更多信息,请参阅 更新日志
贡献
有关详细信息,请参阅 贡献指南
安全
如果您发现任何安全相关的问题,请通过电子邮件 singlephon@gmail.com 而不是使用问题跟踪器。
鸣谢
许可证
MIT许可证(MIT)。有关更多信息,请参阅 许可证文件