【发布时间】:2015-10-17 13:54:27
【问题描述】:
我想使用 Laravel 中的队列将消息推送到队列。因此,我想先尝试基本流程,这会引发错误。
当我在 Laravel 中使用 CommandBus 时,我创建了一个监听器:
监听器 - IncidentNotifier.php
<?php
namespace App\Listeners;
use App\Events\Incident\IncidentWasPosted;
use App\Events\EventListener;
use App\Http\Traits\SearchResponder;
use App\Jobs\SendAlarmToResponder;
use Illuminate\Foundation\Bus\DispatchesJobs;
class IncidentNotifier extends EventListener {
use DispatchesJobs, SearchResponder;
public function whenIncidentWasPosted(IncidentWasPosted $event) {
$responders = $this->getResponderInRange($event);
$this->dispatch(new SendAlarmToResponder($responders));
}
}
此侦听器应将作业(尚未完成)排队以使用推送通知服务,因为这会在不使用队列的情况下暂时阻塞我的系统。
工作 - SendToAlarmResponder.php
<?php
namespace App\Jobs;
use App\Jobs\Job;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;
class SendAlarmToResponder extends Job implements SelfHandling, ShouldQueue
{
use InteractsWithQueue, SerializesModels;
protected $responders = array();
public function __construct($responders)
{
$this->$responders = $responders;
}
public function handle($responders)
{
var_dump($responders);
}
}
searchResponder 方法
public function getResponderInRange($event) {
$position[] = array();
$position['latitude'] = $event->incident->latitude;
$position['longitude'] = $event->incident->longitude;
$queryResult = ResponderHelper::searchResponder($position);
return $queryResult;
}
响应者数组是我想传递给作业以便稍后在那里处理的变量。这是我从数据库中收到的一组对象,效果很好。但我收到错误消息:
ErrorException in SendAlarmToResponder.php line 19:
Array to string conversion
我怎样才能把这个数组交给工作?
【问题讨论】: