”工欲善其事,必先利其器。“—孔子《论语.录灵公》
首页 > 编程 > 集中您的通知和作业处理

集中您的通知和作业处理

发布于2024-11-08
浏览:150

Centralize your notification and job handling

为了简化在各种事件(如用户创建、密码重置等)后发送多封电子邮件通知的过程,您可以采取一些步骤来集中您的通知和作业处理。这种方法将使您的工作更轻松且更具可扩展性,而无需为每个事件创建单独的作业或通知。

简化电子邮件通知处理的策略:

  1. 使用通用电子邮件通知作业
  2. 利用事件监听器架构.
  3. 对类似通知进行分组.

1. 创建通用电子邮件通知作业

您可以创建一个单个可重用作业,将通知和用户作为参数,而不是为每个通知创建单独的作业。这样,同一个作业可以用来处理不同的通知。

通用 SendEmailNotificationJob:

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Notifications\Notification;
use App\Models\User;

class SendEmailNotificationJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $user;
    public $notification;

    /**
     * Create a new job instance.
     *
     * @param  User $user
     * @param  Notification $notification
     * @return void
     */
    public function __construct(User $user, Notification $notification)
    {
        $this->user = $user;
        $this->notification = $notification;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        // Send the notification
        $this->user->notify($this->notification);
    }
}

通过此通用作业,您可以使用同一作业发送不同类型的电子邮件通知:

用法示例:

use App\Jobs\SendEmailNotificationJob;
use App\Notifications\UserWelcomeNotification;
use App\Models\User;

$user = User::find(1); // Example user

// Dispatch a welcome email notification
SendEmailNotificationJob::dispatch($user, new UserWelcomeNotification());

// Dispatch a password reset notification
SendEmailNotificationJob::dispatch($user, new PasswordResetNotification());

2. 利用事件监听器架构

Laravel 的事件监听器架构允许您根据特定事件(例如用户创建)自动触发通知和作业,而不是在每个事件后手动调度作业。

第 1 步:定义事件:

您可以定义一个事件,例如UserCreated:

php artisan make:event UserCreated

用户创建事件示例:

namespace App\Events;

use App\Models\User;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class UserCreated
{
    use Dispatchable, SerializesModels;

    public $user;

    public function __construct(User $user)
    {
        $this->user = $user;
    }
}

第2步:创建监听器:

您可以创建一个监听器,在事件触发时发送通知:

php artisan make:listener SendUserWelcomeNotification --event=UserCreated

监听器示例:

namespace App\Listeners;

use App\Events\UserCreated;
use App\Jobs\SendEmailNotificationJob;
use App\Notifications\UserWelcomeNotification;

class SendUserWelcomeNotification
{
    public function handle(UserCreated $event)
    {
        // Dispatch the email notification job
        SendEmailNotificationJob::dispatch($event->user, new UserWelcomeNotification());
    }
}

第 3 步:创建用户时触发事件:

每当创建用户时,您都可以触发该事件,Laravel 将自动处理其余的事情:

use App\Events\UserCreated;

$user = User::create($data);
event(new UserCreated($user));

这种方法允许您将处理通知的逻辑与业务逻辑解耦,从而使系统更具可扩展性。

3. 相似通知分组

如果您有许多类似的通知(例如,欢迎电子邮件、密码重置等与用户相关的通知),您可以创建一个通知服务来集中处理所有用户通知。

通知服务示例:

namespace App\Services;

use App\Models\User;
use App\Jobs\SendEmailNotificationJob;
use App\Notifications\UserWelcomeNotification;
use App\Notifications\PasswordResetNotification;

class NotificationService
{
    public function sendUserWelcomeEmail(User $user)
    {
        SendEmailNotificationJob::dispatch($user, new UserWelcomeNotification());
    }

    public function sendPasswordResetEmail(User $user)
    {
        SendEmailNotificationJob::dispatch($user, new PasswordResetNotification());
    }

    // You can add more methods for different types of notifications
}

用法示例:

在您的控制器或事件侦听器中,您现在可以简单地调用该服务:

$notificationService = new NotificationService();
$notificationService->sendUserWelcomeEmail($user);

结论:

  • 单一作业:您可以使用通用作业(SendEmailNotificationJob)来处理不同类型的通知。
  • 事件监听器架构:利用Laravel的事件监听器系统根据系统事件自动触发通知。
  • 集中通知服务:将类似的通知分组到一个服务中,以实现更好的管理和可重用性。

这种方法有助于保持代码干燥(不要重复自己),并且当您有多个电子邮件通知要发送时,可以更轻松地进行维护。

版本声明 本文转载于:https://dev.to/msnmongare/centralize-your-notification-and-job-handling-1oid?1如有侵犯,请联系[email protected]删除
最新教程 更多>

免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。

Copyright© 2022 湘ICP备2022001581号-3