kettasoft/booter
Composer 安装命令:
composer require kettasoft/booter
包简介
The Laravel Model Event Booter package provides a flexible way to organize and manage model event handling in Laravel applications. It allows developers to define specific actions for model events (such as created, updated, deleted, etc.) by associating them with custom classes. The package simplifi
README 文档
README
Laravel Model Event Booter is a package that simplifies managing model events in Laravel by allowing you to define and map model events (e.g., created, updated, deleted, etc.) to custom classes. These classes handle the logic associated with those events, making your code cleaner, modular, and easier to maintain.
Features
- Automatically boot and trigger model events with custom logic.
- Organize event-specific logic into separate, reusable classes.
- Works with Laravel's built-in model events (
created,updated,deleted, etc.). - Easy-to-use HasBooter trait that handles event firing.
Installation
-
- Install the package via Composer:
composer require kettasoft/booter
-
- Add the
HasBootertrait to any model where you want to manage events.
- Add the
-
- Define the
$eventsarray in the model to map events to the classes that will handle them.
- Define the
-
- Publish the package's configuration file by running:
php artisan vendor:publish --provider="Scaffolding\Booter\Providers\BooterServiceProvider" --tag=config
Usage
-
-
Add the
HasBooterTrait to Your Model -
Use the
HasBootertrait in your model to enable event handling:
-
use Scaffolding\Booter\Traits\HasBooter; class Post extends Model { use HasBooter; /** * The event-to-class mappings. * * @var array */ protected static $events = [ 'created' => [ \App\Boot\AttachAuthorIdBoot::class, ], 'updated' => [ \App\Boot\LogChangesBoot::class, ], ]; }
-
- Create Event Handler Classes Create a class for each event that you want to handle. Each class should have a handle method where you define the logic to run when the event is triggered.
namespace App\Boots; use Scaffolding\Booter\HasBooter; class AttachAuthorIdBoot extends HasBooter { /** * Handle the model event. * * @param \Illuminate\Database\Eloquent\Model $model * @return void */ public function handle(\Illuminate\Database\Eloquent\Model $model) { // Custom logic for 'created' event $model->author_id = auth()->id(); $model->save(); } }
The class defined in the event will be called automatically when the event occurs.
-
- Handle Multiple Events
You can define multiple events in the $events array for a single model. Each event can have one or more classes that will be triggered in sequence.
protected static $events = [ 'created' => [ \App\Boot\AttachAuthorIdBoot::class, \App\Boot\SendNotificationBoot::class, ], 'updated' => [ \App\Boot\LogChangesBoot::class, ], ];
-
-
Event Handling Flow
-
When the model event (like
created,updated, etc.) is triggered, the package automatically fires the associated class. -
Each class must have a handle method where you implement the custom logic.
-
Contributing
Contributions are welcome! If you find any issues or have ideas for improvements, feel free to submit a pull request or open an issue.
License
This package is open-source software licensed under the MIT license.
kettasoft/booter 适用场景与选型建议
kettasoft/booter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 10 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 kettasoft/booter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kettasoft/booter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 19
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 11
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-10-01