【问题标题】:Laravel 5.2 Queuing eventsLaravel 5.2 排队事件
【发布时间】:2016-09-21 17:18:09
【问题描述】:

我有一些执行 CRUD、上传和电子邮件的事件。我想对这些事件进行排队。我做了一些研究,发现我需要做的就是配置我的队列驱动程序并在我的事件监听器中实现 ShouldQueue 契约。因为我使用 redis 作为我的缓存和会话驱动程序,所以我继续使用 redis 作为我的队列驱动程序。当我在不使用 ShouldQueue 的情况下触发事件时,一切正常,我收到电子邮件,数据被添加到 DB ..etc。但是,当我将 ShouldQueue 添加到我的事件侦听器并运行 queue:listen 时,我触发了我的事件,但队列中没有添加或执行任何内容。我也没有收到任何电子邮件,DB ...等。我错过了什么?这是我的代码示例:

.env 文件

QUEUE_DRIVER=redis

App\Events\ContactUs\CustomerContactUs

use SerializesModels;
/**
 * Create a new event instance.
 */
public function __construct()
{

}

App\Listeners\ContactedUs\SendCustomerEmailToUs

private $mailer;
private $request;    
public function __construct(Mailer $mailer, Request $request)
{
    $this->mailer = $mailer;
    $this->request = $request;
}
public function handle(CustomerContactedBullard $event)
{
....   
$this->mailer->send('public.emails.contact_us',$this->request->all(),$params);
}

ContactUsController

event(new ContactedUs\CustomerContactedUs());

我运行的终端

php artisan queue:listen

谢谢

【问题讨论】:

    标签: php laravel-5 queue laravel-5.2


    【解决方案1】:

    您应该像这样在 EventServiceProvider 中添加有效条目:

    protected $listen = [
       \App\Events\ContactUs\CustomerContactUs:class => [
          \App\Listeners\ContactedUs\SendCustomerEmailToUs::class,
       ],
    ];
    

    【讨论】:

    • 抱歉忘了提到我已将这些类添加到 EventServiceProvider。我发现了我的问题。我在我的听众中使用 request->get 。我必须通过事件构造函数传递表单数据。我现在遇到的问题是我的队列无法访问我的模型的全局范围。当我查询模型时,我有一个全局范围集。队列无法访问它,因此我无法在查询中不使用 withoutGlobalScopes() 进行查询
    猜你喜欢
    • 2016-06-05
    • 1970-01-01
    • 1970-01-01
    • 2017-06-10
    • 2016-05-19
    • 2012-07-10
    • 2020-10-05
    • 2016-05-16
    • 2019-08-22
    相关资源
    最近更新 更多