承接 waad/laravel-dynamic-observer 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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 $observer property
  • 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 我们能提供哪些服务?
定制开发 / 二次开发

基于 waad/laravel-dynamic-observer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 147
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 31
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-06-05