To simplify sending multiple email notifications after various events (like user creation, password resets, etc.), you can take a few steps tocentralize your notification and job handling. This approach will make your work easier and more scalable without having to create a separate job or notification for each event.
Instead of creating separate jobs for each notification, you can create asingle reusable jobthat takes the notification and the user as parameters. This way, the same job can be used to handle different notifications.
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); } }
With this generalized job, you can dispatch different types of email notifications using the same job:
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());
Instead of manually dispatching jobs after each event, Laravel’sevent-listener architectureallows you to automatically trigger notifications and jobs based on specific events (like user creation).
You can define an event such as 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; } }
You can create a listener that sends a notification when the event is fired:
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()); } }
Whenever a user is created, you can fire the event, and Laravel will automatically handle the rest:
use App\Events\UserCreated; $user = User::create($data); event(new UserCreated($user));
This approach allows you to decouple the logic of handling notifications from your business logic, making the system more scalable.
If you have many similar notifications (e.g., user-related notifications like welcome emails, password resets, etc.), you can create aNotification Servicethat handles all user notifications in a centralized way.
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 }
In your controllers or event listeners, you can now simply call the service:
$notificationService = new NotificationService(); $notificationService->sendUserWelcomeEmail($user);
This approach helps keep your code DRY (Don’t Repeat Yourself) and makes it easier to maintain when you have multiple email notifications to send.
The above is the detailed content of Centralize your notification and job handling. For more information, please follow other related articles on the PHP Chinese website!