Home > Backend Development > PHP Tutorial > laravel Jobs 队列处理

laravel Jobs 队列处理

WBOY
Release: 2016-06-06 20:30:55
Original
1654 people have browsed it

<code>php artisan make:job SendEmail --queued
</code>
Copy after login
Copy after login

生成后 在App\Jobs

<code><?php namespace App\Jobs;
use App\User;
use App\Jobs\Job;
use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;

class SendEmail extends Job implements SelfHandling, ShouldQueue
{
    use InteractsWithQueue, 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,$email)
    {

        $mailer->send('emails.reminder', ['user' => $this->user], function ($m){
            $m->to($email, 'zan')->subject('发送成功');
        });


    }
}

</code>
Copy after login
Copy after login

在控制器里这么用;

<code>    $email = "onm@163.com";
    $job = (new SendEmail($user,$email))->delay(260);
    $this->dispatch($job);
</code>
Copy after login
Copy after login

在这里 传email 到 handle方法里面;

可handle 方法里 根本获取不到 $email 这个变量

Jobs 队列处理 如何传入更多数据呢 在控制器里传入

求大神指导一下呀

回复内容:

<code>php artisan make:job SendEmail --queued
</code>
Copy after login
Copy after login

生成后 在App\Jobs

<code><?php namespace App\Jobs;
use App\User;
use App\Jobs\Job;
use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;

class SendEmail extends Job implements SelfHandling, ShouldQueue
{
    use InteractsWithQueue, 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,$email)
    {

        $mailer->send('emails.reminder', ['user' => $this->user], function ($m){
            $m->to($email, 'zan')->subject('发送成功');
        });


    }
}

</code>
Copy after login
Copy after login

在控制器里这么用;

<code>    $email = "onm@163.com";
    $job = (new SendEmail($user,$email))->delay(260);
    $this->dispatch($job);
</code>
Copy after login
Copy after login

在这里 传email 到 handle方法里面;

可handle 方法里 根本获取不到 $email 这个变量

Jobs 队列处理 如何传入更多数据呢 在控制器里传入

求大神指导一下呀

<code>php</code><code>$mailer->send('emails.reminder', ['user' => $this->user], function ($m) use($email){
            $m->to($email, 'zan')->subject('发送成功');
        });
</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template