流程:php
php artisan make:event UserLogin
LoginController.phplaravel
/** * 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)); }
php artisan make:listener EmailAdminUserLogin --event=UserLogin
//应用程序的事件监听器映射 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
数据库
protected function schedule(Schedule $schedule) { $schedule->call(function (){ \Log::info('我是call方法实现的定时任务'); })->everyMinute(); }
执行:php artisan schedule:run
编程
生成命令:php artisan make:command SayHello
app
<?php namespace 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.phpide
protected function schedule(Schedule $schedule) { $schedule->command('message:hi') ->everyMinute(); }
执行:php artisan schedule:run
函数
QUEUE_DRIVER=database
this
如:数据库驱动spa
php artisan queue:table php artisan migrate
生成任务类:code
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); } }
你写好任务类后,就能经过 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个队列任务:
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 队列