1.1 Create event
php artisan make:event UserLogin
/** * 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 Create listener1.2.1 Method 1: Manually create
php artisan make:listener EmailAdminUserLogin --event=UserLogin
1.2.2 Method 2:
Recommend the following method:Automatically generate events and listeners
//应用程序的事件监听器映射 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) { // }); } }
Generate events & listeners:
php artisan event:generate2. Laravel’s task scheduling (planned task) function Task Scheduling2.1 call method
protected function schedule(Schedule $schedule) { $schedule->call(function (){ \Log::info('我是call方法实现的定时任务'); })->everyMinute(); }
2.2 crontab method
Kernel.php
protected function schedule(Schedule $schedule) { $schedule->command('message:hi') ->everyMinute(); }
Execution:
php artisan schedule:run3. Queue tasks3.1 Necessary driver settings
For example: database driver
php artisan queue:table php artisan migrate
3.2 Create task
Generate task class: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 Distribute taskAfter you write the task class, you can distribute it through the
dispatchauxiliary function. The only parameter that needs to be passed to
dispatchis an instance of this task class:Use the model factory to generate 30 users:
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');
Database tablejobs
Generate 5 queue tasks:##3.4 Run queue processor
php artisan queue:work
Tips:
Be aware that once thequeue:work
command starts, it will run until you manually stop it or you close the consoleProcessing a single Task:You can use the
--once
php artisan queue:work --once
Extension:
UseBeanstalkd
to manage the queue,Supervisoris used to monitor the tasks in the queue, and automatically help us execute them if there are tasks in the queue, eliminating the need to manually typephp artisancommand to ensure that your queue can be executed correctly
《Related recommendations:
The latest five Laravel video tutorials》