一、事件監聽
流程:
1.1 創建event
php artisan make:event UserLogin
LoginController.php
/*** The user has been authenticated.** @param \Illuminate\Http\Request $request* @param mixed $user* @return mixed*/protected function authenticated(Request $request, $user){event(new UserLogin($user));}
1.2 創建listener
1.2.1 方式一:手動創建
php artisan make:listener EmailAdminUserLogin --event=UserLogin
1.2.2 方式二:推薦如下方式:自動生成事件和監聽
//應用程序的事件監聽器映射class EventServiceProvider extends ServiceProvider
{/*** The event listener mappings for the application.** @var array*/protected $listen = ['App\Events\UserLogin' => ['App\Listeners\UserLogin\EmailAdminUserLogin','App\Listeners\UserLogin\TraceUser','App\Listeners\UserLogin\AddUserLoginCounter',],'App\Events\UserLogout' => ['App\Listeners\UserLogout\EmailAdminUserLogout','App\Listeners\UserLogout\TraceUser',],];/*** Register any events for your application.** @return void*/public function boot(){parent::boot();Event::listen('event.*', function ($eventName, array $data) {//});}
}
生成事件 & 監聽器:php artisan event:generate
二、Laravel 的任務調度(計劃任務)功能 Task Scheduling
2.1 call方式
protected function schedule(Schedule $schedule){$schedule->call(function (){\Log::info('我是call方法實現的定時任務');})->everyMinute();}
執行:php artisan schedule:run
2.2 crontab方式
2.2 command方式
生成命令:php artisan make:command SayHello
<?phpnamespace App\Console\Commands;use Illuminate\Console\Command;class SayHello extends Command
{/*** The name and signature of the console command.** @var string*/protected $signature = 'message:hi';/*** The console command description.** @var string*/protected $description = 'Command description';/*** Create a new command instance.** @return void*/public function __construct(){parent::__construct();}/*** Execute the console command.** @return mixed*/public function handle(){//書寫處理邏輯\Log::info('早上好,用戶');}
}
Kernel.php
protected function schedule(Schedule $schedule)
{$schedule->command('message:hi')->everyMinute();
}
執行:php artisan schedule:run
三、隊列任務
3.1 驅動的必要設置
????QUEUE_DRIVER=database
如:數據庫驅動
php artisan queue:tablephp artisan migrate
3.2 創建任務
???? 生成任務類:
php artisan make:job SendReminderEmail
class SendReminderEmail implements ShouldQueue
{use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;public $user;/*** Create a new job instance.** @param User $user*/public function __construct(User $user){$this->user = $user;}/*** Execute the job.** @return void*/public function handle(){\Log::info('send reminder email to user' . $this->user->email);}
}
3.3 分發任務
????你寫好任務類后,就能通過 dispatch
輔助函數來分發它了。唯一需要傳遞給 dispatch
的參數是這個任務類的實例:
利用模型工廠生成30個用戶:
public function store(Request $request){$users = User::where('id','>',24)->get();foreach ($users as $user){$this->dispatch(new SendReminderEmail($user));}return 'Done';}
Route::get('/job', 'UserController@store');
數據庫表jobs
生成5個隊列任務:
3.4 運行隊列處理器
php artisan queue:work
Tips:要注意,一旦 queue:work
命令開始,它將一直運行,直到你手動停止或者你關閉控制臺
處理單一任務:你可以使用 --once
選項來指定僅對隊列中的單一任務進行處理
php artisan queue:work --once
拓展:使用 Beanstalkd
管理隊列,Supervisor
則是用來監聽隊列的任務,并在隊列存在任務的情況下自動幫我們去執行,免去手動敲 php artisan
的命令,保證自己的隊列可以正確執行
致謝
謝謝你看到這里,有什么問題可以在評論區留言交流,謝謝!
參考文檔:Laravel5.5 的事件系統
參考視頻01:Coding 10編程原動力 - Laravel5.5 事件監聽
參考視頻02:Coding 10編程原動力 - Laravel5.5 定時任務
參考視頻03:輕松使用 Laravel 隊列