Laravel 8 響應發(fā)送到瀏覽器后的調度

2021-07-19 11:00 更新

另外, dispatchAfterResponse 方法會延遲發(fā)送任務,直到將響應發(fā)送到用戶的瀏覽器之后。這仍然允許用戶開始使用應用程序,即使隊列任務仍然在執(zhí)行。這通常只適用于需要 1 秒鐘的任務,比如發(fā)送電子郵件:

use App\Jobs\SendNotification;

SendNotification::dispatchAfterResponse(); 

你可以 dispatch 一個閉包,并將 afterResponse 方法鏈到幫助程序上,在響應發(fā)送到瀏覽器后執(zhí)行閉包:

use App\Mail\WelcomeMessage;
use Illuminate\Support\Facades\Mail;

dispatch(function () {
    Mail::to('taylor@laravel.com')->send(new WelcomeMessage);
})->afterResponse(); 
以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號