>백엔드 개발 >PHP 튜토리얼 >laravel Jobs 队列处理

laravel Jobs 队列处理

WBOY
WBOY원래의
2016-06-06 20:30:551644검색

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

生成后 在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>

在控制器里这么用;

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

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

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

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

求大神指导一下呀

回复内容:

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

生成后 在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>

在控制器里这么用;

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

在这里 传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>
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.