如何在laravel5 中使用queue队列
Laravel Queue是延迟处理应用程序中耗时任务的有效方法。此类任务的示例可能包括每当新用户在您的应用程序中注册或通过社交媒体分享帖子时发送验证电子邮件。Laravel队列有助于延迟这些任务,从而提高Web应用程序向您的应用程序的速度。
实例说明
我们将构建一个简单的博客应用程序,允许您提交帖子并在创建新帖子时发送电子邮件。我们将利用队列来处理电子邮件的发送
1. 让我们创建我们的模型,控制器和迁移
php artisan make:model Post -m -c
- 1
提示:该 -m 和 -c 标志将创建一个模型为您相关的迁移和控制文件。
数据迁移文件:
<?php
use Illuminate<span class=“hljs-title”>Support<span class=“hljs-title”>Facades<span class=“hljs-title”>Schema;
use Illuminate<span class=“hljs-title”>Database<span class=“hljs-title”>Schema<span class=“hljs-title”>Blueprint;
use Illuminate<span class=“hljs-title”>Database<span class=“hljs-title”>Migrations<span class=“hljs-title”>Migration;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create(‘posts’, function (Blueprint KaTeX parse error: Expected '}', got 'EOF' at end of input: …hljs-variable">table->increments(‘id’);
KaTeX parse error: Expected 'EOF', got '&' at position 14: table</span>-&̲gt;string(<span…table->string(‘body’);
$table->timestamps();
});
}
<span class="hljs-comment">/**
* Reverse the migrations.
*
*<span class="hljs-phpdoc"> @return</span> void
*/</span>
<span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">down</span><span class="hljs-params">()</span>
{<!-- --></span>
Schema::dropIfExists(<span class="hljs-string">'posts'</span>);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
2. 添加
队列表Laravel提供了对队列使用不同驱动程序的支持。您可以在这里看到Laravel支持的队列驱动程序列表。为了本教程的目的,我们将使用数据库作为我们的队列驱动程序。因此,我们需要一个表来保存这些工作。要生成创建此表的迁移,请运行以下命令:
php artisan queue:table
这将为名为jobs的表创建迁移文件,因为我们所有的队列都将记录在名为jobs的表上。我们需要告诉Laravel,我们将在.env
文件中使用数据库作为队列驱动程序。
QUEUE_DRIVER=database
另外,使用数据库详细信息编辑.env文件并使用以下命令运行迁移:
注:根据实际情况配置自己的数据信息
使用以下命令运行迁移:
php artisan migrate
3. 发送电子邮件
由于我们的应用程序将要求我们发送电子邮件,因此我们为发展目的设置邮件提供商。
案例我们使用163提供的smtp服务
MAIL_DRIVER=smtp
MAIL_HOST=smtp.163.com // SMTP服务器: smtp.163.com
MAIL_PORT=25
MAIL_USERNAME=[username] // 邮箱帐号
MAIL_PASSWORD=[password] // 客户端授权密码 注:不是登录密码
MAIL_ENCRYPTION=null
- 1
- 2
- 3
- 4
- 5
- 6
4. 设置路由
我们的应用程序只有两条路线。使用以下代码编辑 web/routes.php 文件:
Route::get('/index', 'PostController@index'); Route::post('/posts', 'PostController@store');
- 1
5. 控制器方法和视图
<?php
namespace App<span class=“hljs-title”>Http<span class=“hljs-title”>Controllers;
use Illuminate<span class=“hljs-title”>Http<span class=“hljs-title”>Request;
use App<span class=“hljs-title”>Jobs<span class=“hljs-title”>SendPostEmail;
use App<span class=“hljs-title”>Post;
class PostController extends Controller
{
public function index()
{
return view(‘index’);
}
<span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">store</span><span class="hljs-params">(Request <span class="hljs-variable">$request</span>)</span>
{<!-- --></span>
<span class="hljs-variable">$request</span>->validate([
<span class="hljs-string">'title'</span>=><span class="hljs-string">'required|min:6'</span>,
<span class="hljs-string">'body'</span>=> <span class="hljs-string">'required|min:6'</span>,
]);
<span class="hljs-variable">$post</span> = <span class="hljs-keyword">new</span> Post;
<span class="hljs-variable">$post</span>->title = <span class="hljs-variable">$request</span>->title;
<span class="hljs-variable">$post</span>->body = <span class="hljs-variable">$request</span>->body;
<span class="hljs-variable">$post</span>->save();
<span class="hljs-variable">$this</span>->dispatch(<span class="hljs-keyword">new</span> SendPostEmail(<span class="hljs-variable">$post</span>)); <span class="hljs-comment">// 队列</span>
<span class="hljs-keyword">return</span> redirect()->back()->with(<span class="hljs-string">'status'</span>, <span class="hljs-string">'Your post has been submitted successfully'</span>);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
索引方法返回一个我们将很快创建的索引视图。store方法对我们的请求输入执行一组验证规则,并将记录存储到我们的数据库中,让我们创建添加帖子的表单。在resources/views目录中,创建一个index.blade.php文件并使用以下代码对其进行编辑
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width">
<title>Laravel Queues</title>
<meta name="description" content="">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div class=“container”>
<div class=“col-md-5”>
<h4 class=“page-header”>Laravel Queues </h4>
@foreach ($errors->all() as $error)
<p class=“alert alert-danger”>{
{ $error }}</p>
@endforeach
@if (session(‘status’))
<div class=“alert alert-success”>
{
{ session(‘status’)}}
</div>
@endif
<form class=“form-vertical” role=“form” method=“post” action="/posts">
{
{csrf_field()}}
<div class=“form-group”>
<input type=“text” name=“title” class=“form-control” placeholder=“Title”>
</div>
<div class=“form-group”>
<textarea class=“form-control” rows=“5” name=“body” class=“form-control” placeholder=“Content”></textarea>
</div>
<div class=“form-group”>
<button type=“submit” class=“btn btn-info”>Submit Post</button>
</div>
</form>
</div>
</div>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
您可以启动本地服务器并导航到浏览器中的/ index路径以查看我们刚刚创建的表单。
6.使用队列发送电子邮件
让我们创建排队的作业,以便在提交新帖子时发送电子邮件。运行以下命令:
php artisan make:job SendPostEmail
这将在App \ Jobs目录中为我们创建一个SendPostEmail类。该类只包含一个handle()方法,在队列处理作业时调用它。在SendPostEmail文件的顶部,导入以下依赖项:
use Mail;
use App\Post
- 1
- 2
SendPostEmail.php
<?php
namespace App<span class=“hljs-title”>Jobs;
use Illuminate<span class=“hljs-title”>Bus<span class=“hljs-title”>Queueable;
use Illuminate<span class=“hljs-title”>Queue<span class=“hljs-title”>SerializesModels;
use Illuminate<span class=“hljs-title”>Queue<span class=“hljs-title”>InteractsWithQueue;
use Illuminate<span class=“hljs-title”>Contracts<span class=“hljs-title”>Queue<span class=“hljs-title”>ShouldQueue;
use Illuminate<span class=“hljs-title”>Foundation<span class=“hljs-title”>Bus<span class=“hljs-title”>Dispatchable;
use Mail;
use App<span class=“hljs-title”>Post;
class SendPostEmail implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
<span class="hljs-keyword">protected</span> <span class="hljs-variable">$post</span>;
<span class="hljs-comment">/**
* Create a new job instance.
*
*<span class="hljs-phpdoc"> @return</span> void
*/</span>
<span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">__construct</span><span class="hljs-params">(Post <span class="hljs-variable">$post</span>)</span>
{<!-- --></span>
<span class="hljs-comment">//</span>
<span class="hljs-variable">$this</span>->post = <span class="hljs-variable">$post</span>;
}
<span class="hljs-comment">/**
* Execute the job.
*
*<span class="hljs-phpdoc"> @return</span> void
*/</span>
<span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">handle</span><span class="hljs-params">()</span>
{<!-- --></span>
<span class="hljs-comment">//</span>
<span class="hljs-variable">$data</span>= <span class="hljs-keyword">array</span>(
<span class="hljs-string">'title'</span>=> <span class="hljs-variable">$this</span>->post->title,
<span class="hljs-string">'body'</span>=> <span class="hljs-variable">$this</span>->post->body,
);
<span class="hljs-comment">// emails.post 对应的视图文件模板</span>
Mail::send(<span class="hljs-string">'emails.post'</span>, <span class="hljs-variable">$data</span>, <span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">(<span class="hljs-variable">$message</span>)</span>{<!-- --></span>
<span class="hljs-variable">$message</span>->from(<span class="hljs-string">'************'</span>, <span class="hljs-string">'Laravel Queues'</span>);
<span class="hljs-variable">$message</span>->to(<span class="hljs-string">'************'</span>)->subject(<span class="hljs-string">'There is a new post'</span>);
});
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
我们创建了一组数据,其中包含每篇文章的标题和正文,以传递给我们将很快创建的电子邮件视图。接下来,我们使用邮件Façade上的send方法接受三个参数,让我们快速创建电子邮件的视图。在resources \ views目录下,创建一个emails文件夹,然后在文件夹内创建一个post.blade.php。将以下代码添加到刚刚创建的文件中:
1)我们想要使用的视图的名称发送电子邮件。
2)传递给电子邮件的一组数据。
3)关闭来定制我们的电子邮件主题,发件人和收件人
resources/views/emails/post.blade.php:
<p> A new post has been created </p>
<p> {
!! $title !!}</p>
<p> {
!! $body !!} </p>
- 1
- 2
- 3
7.调度作业
让我们将排队的作业添加到PostController中。在PostController的顶部,导入SendPostEmail类
use App\Jobs\SendPostEmail;
派发方法派发队列任务
$this->dispatch(new SendPostEmail($post));
- 1
8.监听队列
要让我们的应用程序监听队列,请运行以下命令
php artisan queue:work
注意:令一旦启动,它将继续运行,直到您手动停止或关闭终端
9. 运行效果
现在,您可以尝试提交帖子
命令检测行:
接收邮件情况: