承接 ez-php/notification 相关项目开发

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

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

ez-php/notification

最新稳定版本:1.11.0

Composer 安装命令:

composer require ez-php/notification

包简介

Multi-channel notification orchestration for the ez-php framework — mail, broadcast, and database channels with optional queue-backed async delivery

README 文档

README

Multi-channel notification orchestration for ez-php applications. Routes a single notification to any combination of mail, broadcast, and database channels. Optionally dispatches deliveries asynchronously via the queue.

Installation

composer require ez-php/notification

Register the provider in provider/modules.php:

EzPhp\Notification\NotificationServiceProvider::class,

Quick Start

1 — Define a notifiable (e.g. your User model)

use EzPhp\Notification\NotifiableInterface;

class User implements NotifiableInterface
{
    public function __construct(
        public readonly int    $id,
        public readonly string $email,
    ) {}

    public function routeNotificationFor(string $channel): string|int
    {
        return match ($channel) {
            'mail'      => $this->email,
            'broadcast' => 'users.' . $this->id,
            'database'  => $this->id,
        };
    }
}

2 — Define a notification

use EzPhp\Mail\Mailable;
use EzPhp\Notification\Channel\ToMailInterface;
use EzPhp\Notification\NotifiableInterface;
use EzPhp\Notification\NotificationInterface;

class WelcomeNotification implements NotificationInterface, ToMailInterface
{
    public function via(): array
    {
        return ['mail'];
    }

    public function toMail(NotifiableInterface $notifiable): Mailable
    {
        return (new Mailable())
            ->to((string) $notifiable->routeNotificationFor('mail'))
            ->subject('Welcome!')
            ->text('Thanks for signing up.');
    }
}

3 — Send it

use EzPhp\Notification\Notification;

Notification::send($user, new WelcomeNotification());

Channels

mail

Delivers via ez-php/mail. The notification must implement ToMailInterface:

public function toMail(NotifiableInterface $notifiable): Mailable;

broadcast

Delivers via ez-php/broadcast. The notification must implement ToBroadcastInterface:

public function broadcastOn(NotifiableInterface $notifiable): string;   // channel name
public function broadcastAs(NotifiableInterface $notifiable): string;   // event name
public function broadcastWith(NotifiableInterface $notifiable): array;  // payload

database

Persists to the notifications table. The notification must implement ToDatabaseInterface:

public function toDatabase(NotifiableInterface $notifiable): array;  // JSON payload

The table is auto-created on first use. To create it via migration instead:

-- MySQL
CREATE TABLE notifications (
    id              INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    type            VARCHAR(255) NOT NULL,
    notifiable_type VARCHAR(255) NOT NULL,
    notifiable_id   VARCHAR(255) NOT NULL,
    data            JSON         NOT NULL,
    read_at         DATETIME     NULL,
    created_at      DATETIME     NOT NULL
);

-- SQLite
CREATE TABLE notifications (
    id              INTEGER PRIMARY KEY AUTOINCREMENT,
    type            TEXT NOT NULL,
    notifiable_type TEXT NOT NULL,
    notifiable_id   TEXT NOT NULL,
    data            TEXT NOT NULL,
    read_at         TEXT NULL,
    created_at      TEXT NOT NULL
);

Multi-channel

Return multiple channels from via() and implement the matching interfaces:

class OrderShippedNotification implements
    NotificationInterface,
    ToMailInterface,
    ToBroadcastInterface,
    ToDatabaseInterface
{
    public function via(): array
    {
        return ['mail', 'broadcast', 'database'];
    }

    // toMail(), broadcastOn(), broadcastAs(), broadcastWith(), toDatabase() ...
}

Async delivery (queue)

Add ShouldQueueInterface to defer mail and broadcast channels via the queue:

use EzPhp\Notification\ShouldQueueInterface;

class WelcomeNotification implements NotificationInterface, ShouldQueueInterface, ToMailInterface
{
    // ...
}

When QueueInterface is bound (i.e. QueueServiceProvider is registered), the Notifier pushes SendMailNotificationJob / SendBroadcastNotificationJob onto the queue instead of delivering synchronously. The database channel always runs synchronously.

To force synchronous delivery regardless of ShouldQueueInterface:

Notification::sendNow($user, new WelcomeNotification());

Service Provider wiring

NotificationServiceProvider registers the following channels automatically:

Channel Requires Optional
mail MailServiceProvider
broadcast BroadcastServiceProvider
database DatabaseServiceProvider yes — omitted if not bound

Queue support (ShouldQueueInterface) is also optional — if QueueServiceProvider is not registered, all notifications are sent synchronously.

Testing

Inject a real Notifier with spy channels in your tests:

use EzPhp\Notification\Notification;
use EzPhp\Notification\Notifier;

protected function setUp(): void
{
    $this->spyChannel = new SpyChannel();
    Notification::setNotifier(new Notifier(['mail' => $this->spyChannel]));
}

protected function tearDown(): void
{
    Notification::resetNotifier();
}

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-28

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固