waad/laravel-dynamic-observer
Composer 安装命令:
composer require waad/laravel-dynamic-observer
包简介
Call observer of the model without requiring in any provider, support multi observers
README 文档
README
Register model observers dynamically without service providers. Supports single and multiple observers.
Automatically connect your Laravel models to observers using traits or attributes — no service provider required.
Features
- Automatic observer registration via trait
- Custom observer support with
$observerproperty - Multiple observers support
- PHP 8.0+ attribute-based configuration
- Zero configuration needed for convention-based observers
Requirements
- PHP 8.0+
- Laravel 8.0+
Installation
composer require waad/laravel-dynamic-observer
Quick Start
Add the HasObserver trait to your model:
use Illuminate\Database\Eloquent\Model; use Waad\Observer\HasObserver; class Post extends Model { use HasObserver; }
The observer will beauto-detected based on naming convention (PostObserver in App\Observers).
Usage
1. Automatic Observer (Convention-Based)
The observer is automatically detected by naming convention:
// App\Models\Post → App\Observers\PostObserver use Waad\Observer\HasObserver; class Post extends Model { use HasObserver; }
2. Custom Observer
Specify a custom observer class:
use App\Observers\CustomObserver; use Waad\Observer\HasObserver; class Post extends Model { use HasObserver; public static $observer = CustomObserver::class; }
3. Multiple Observers
Register multiple observers:
use App\Observers\FirstObserver; use App\Observers\SecondObserver; use Waad\Observer\HasObserver; class Post extends Model { use HasObserver; public static $observer = [FirstObserver::class, SecondObserver::class]; }
4. Using Attributes (PHP 8.0+)
Use the #[HasObservers] attribute:
use App\Observers\CustomObserver; use Waad\Observer\Attributes\HasObservers; use Waad\Observer\HasObserver; #[HasObservers(CustomObserver::class)] class Post extends Model { use HasObserver; }
Note: When using the attribute, you must also use the trait
HasObserver.
Creating Observers
Generate an observer with Artisan:
php artisan make:observer PostObserver --model=Post
Observer Methods
namespace App\Observers; use App\Models\Post; class PostObserver { public function creating(Post $post) { // Called before creating } public function created(Post $post) { // Called after creating } public function updating(Post $post) { // Called before updating } public function updated(Post $post) { // Called after updating } public function saving(Post $post) { // Called before saving (create or update) } public function saved(Post $post) { // Called after saving } public function deleting(Post $post) { // Called before deleting } public function deleted(Post $post) { // Called after deleting } public function restoring(Post $post) { // Called before restoring (soft deletes) } public function restored(Post $post) { // Called after restoring } public function retrieved(Post $post) { // Called after retrieving } }
Testing
composer test
License
MIT License. See LICENSE for details.
waad/laravel-dynamic-observer 适用场景与选型建议
waad/laravel-dynamic-observer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 147 次下载、GitHub Stars 达 6, 最近一次更新时间为 2023 年 06 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「model」 「observer」 「laravel」 「laravel-observer」 「model-observer」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 waad/laravel-dynamic-observer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 waad/laravel-dynamic-observer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 waad/laravel-dynamic-observer 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
A collection of observer classes that can be use in your Laravel application.
Interfaces for web resource models and services to retrieve and create them
Event dispatcher package
Laravel 5 - Repositories to the database layer
The event package provides functionality for notifying a list of observers about an event.
统计信息
- 总下载量: 147
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 31
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-06-05