1: First we use Composer to install dependencies:
$ composer require "predis/predis:~1.0"
2: Configure the default queue driver as Redis in the configuration file, and the queue configuration file is config/queue.php
:
return [ 'default' => env('QUEUE_DRIVER', 'sync'), 'connections' => [ 'sync' => [ 'driver' => 'sync', ], 'database' => [ 'driver' => 'database', 'table' => 'jobs', 'queue' => 'default', 'expire' => 60, ], 'beanstalkd' => [ 'driver' => 'beanstalkd', 'host' => 'localhost', 'queue' => 'default', 'ttr' => 60, ], 'sqs' => [ 'driver' => 'sqs', 'key' => 'your-public-key', 'secret' => 'your-secret-key', 'queue' => 'your-queue-url', 'region' => 'us-east-1', ], 'iron' => [ 'driver' => 'iron', 'host' => 'mq-aws-us-east-1.iron.io', 'token' => 'your-token', 'project' => 'your-project-id', 'queue' => 'your-queue-name', 'encrypt' => true, ], 'say again' => [ 'driver' => 'redis', 'connection' => 'default', 'queue' => 'default', 'expire' => 60, ], ], 'failed' => [ 'database' => 'mysql', 'table' => 'failed_jobs', ], ];
The first configuration item in the configuration file is default
used to specify the default queue driver, here we change its value to redis
.
3: Create a failed process record table
$ php artisan queue:failed-table $ php artisan migrate
4: Next, create a task class
php artisan make:job SendReminderEmail
After the operation is successful, app/Jobs
one will be generated in the directory SendReminderEmail.php
. We modify its content as follows:
<?php namespace App\Jobs; use App\User; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Mail\Mailer; use Illuminate\Queue\SerializesModels; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Contracts\Queue\ShouldQueue; class SendReminderEmail implements ShouldQueue { use InteractsWithQueue, Queueable, SerializesModels; protected $user; /** * Create a new job instance. * * @return void */ public function __construct(User $user) { $this->user = $user; } /** * Execute the job. * * @return void */ public function handle(Mailer $mailer) { $user = $this->user; $mailer->send('emails.reminder',['user'=>$user],function($message) use ($user){ $message->to($user->email)->subject('New feature release'); }); } }
5: Below we create a partial view of the mail resources/views/emails/reminder.blade.php
:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>test</title> </head> <body> <div class="flex-center position-ref full-height"> The test was successful </div> </body> </html>
6: Write a normal access request in the controller
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\Auth; use Mail; use Storage; use App\User; use App\Jobs\SendReminderEmail; class MailController extends Controller { //Other methods //Send reminder email public function sendReminderEmail(Request $request,$id){ $user = User::findOrFail($id); $this->dispatch(new SendReminderEmail($user)); } }
route
Route::get('/queued/{id}', 'MailController@sendReminderEmail');
7: Open the listening queue
php artisan queue:listen
8: Configure the sending mailbox
9: Open the pop of the mailbox, you can Baidu by yourself