job的handle能否返回值?

$data = $this->dispatch(new PostFormFields());

返回的是0.

PostFormFields是一个jobs类,handle方法能否返回值?

我在handle方法里return 一个数组。结果$data取到是0。

laravel 5.2的版本。

JellyBool

哎呀,这个部分的内容我录好了,没剪上去。。。

leec

@JellyBool 跪求啊!好需要!!
我是照着这个教程做,发现这个问题!
http://laravelacademy.org/post/2358.html

justtest
$this->dispatch(new YourJob($this));
$result = $this->attribute;

class YourJob
{
  func constru(Controller $controller){
     $this->controller = $controller;
  }

  func handle()
  {
    // do something
    $this->controller->attrbute = true;
  }
}
leec

@justtest 构造方法是要传一个controller?

leec

class PostController extends Controller
{

public $postJobReturn;
public function create()
{
    //


    $data = new PostFormFields($this);
    $this->dispatch($data);

    $result = $this->postJobReturn;



    dd($result);

    return view('admin.post.create',$data);
}

Class job {
public function __construct(Controller controller,controller,id = null)
{
$this->id = $id;

    $this->controller = $controller;
}

public function handle()
{
    $fields = $this->fieldList;

    if ($this->id) {
        $fields = $this->fieldsFromModel($this->id, $fields);
    } else {
        $when = Carbon::now()->addHour();
        $fields['publish_date'] = $when->format('M-j-Y');
        $fields['publish_time'] = $when->format('g:i A');
    }

    foreach ($fields as $fieldName => $fieldValue) {
        $fields[$fieldName] = old($fieldName, $fieldValue);
    }


    $this->controller->postJobReturn = array_merge(
        $fields,
        ['allTags' => Tag::lists('tag')->all()]
    );

}

}

Tomoe

@leec
1.建議先在原本你寫的代碼中debug找找return 0的原因
例如把return array_merge....改成return 'hi im return value'之類的,看看$data會抓到什麼東西,或是直接在PostFormFields裡面dd檢查哪裡出問題

2.看了一下那個教程,應該是5.1版本,在5.1 -> 5.2版本中

所有的Job都預設implements SelfHandling,所以可以不用加上去了

3.不建議controller傳進去,會使PostFormFields高耦合於Controller,而且這樣也會使Controller增加許多多餘的屬性,所以還是建議已取得返回值為目標來下手

leec

@Tomoe 嗯!我按照你的“2”,解决了!!非常感谢!原来的类实现的是use Illuminate\Contracts\Queue\ShouldQueue;
把这个删掉就可以了。是怎么回事呢?

Tomoe

@leec

實現這個代表dispatch後會放入Queue(根據你config/queue.php裡面的設定,例如redis)裡面等待排序執行,也就是異步(Async)執行

下面是框架源碼:

    /**
     * Dispatch a command to its appropriate handler.
     *
     * @param  mixed  $command
     * @return mixed
     */
    public function dispatch($command)
    {
        if ($this->queueResolver && $this->commandShouldBeQueued($command)) {
            return $this->dispatchToQueue($command);
        } else {
            return $this->dispatchNow($command);
        }
    }
    /**
     * Dispatch a command to its appropriate handler in the current process.
     *
     * @param  mixed  $command
     * @return mixed
     */
    public function dispatchNow($command)
    {
        return $this->pipeline->send($command)->through($this->pipes)->then(function ($command) {
            return $this->container->call([$command, 'handle']);
        });
    }
    /**
     * Determine if the given command should be queued.
     *
     * @param  mixed  $command
     * @return bool
     */
    protected function commandShouldBeQueued($command)
    {
        return $command instanceof ShouldQueue;
    }

可以很明顯看他會檢查是否有實現ShouldQueue
有 -> 丟入Queue中
沒有 -> 直接 call handle方法

至於為什麼會返回0,似乎是你queue沒設定,導致他push進去queue時失敗,所以返回個0

leec

@Tomoe 原来如此!感谢您的解答!