Laravel 5:从调度命令中检索数据

时间:2015-04-16 15:52:21

标签: php laravel command laravel-5

我已经编写了一个命令UploadPhotoCommand,用于将照片上传到我的应用。该命令不是自我处理的,因此我编写了一个处理程序UploadPhotoCommandHandler来配合它。

我从我的控制器发出命令,如下所示:

<?php

namespace App\Http\Controllers\Photos;

use App\Commands\Photos\UploadPhotoCommand;
use App\Http\Requests\Photos\UploadRequest;

class UploadController {

    public function process(UploadRequest $request)
    {
        try
        {
            $this->dispatch(
                new UploadPhotoCommand($request->file('photo'));
            );
        }
        catch (\Exception $e)
        {
            \Log::error($e);
            return response()->json(['errors' => 'Sorry, something went wrong, please try again later.']);
        }

        // I want to use the ID of the uploaded photo here
        return response()->json(['success' => 'Photo uploaded successfully']);
    }

}

在我的命令处理程序中,我通过移动上传的图像,创建缩略图等并将记录添加到数据库来处理照片上传过程。

我的问题是我想从命令中检索上传照片的ID并使用它。我的目的是在关于上传的照片的回复中返回更多信息,特别是我想使用route函数返回一个URL,并传入照片的ID。

关于如何实现这一目标的任何想法?

2 个答案:

答案 0 :(得分:2)

我找到了一种方法。

为了将来阅读本文的人的利益:

<?php

namespace App\Http\Controllers\Photos;

use App\Commands\Photos\UploadPhotoCommand;
use App\Http\Requests\Photos\UploadRequest;

class UploadController {

    public function process(UploadRequest $request)
    {
        // Initialise the command to a variable
        $command = new UploadPhotoCommand($request->file('photo'));

        try
        {
            // Dispatch the command
            $this->dispatch($command);
        }
        catch (\Exception $e)
        {
            \Log::error($e);
            return response()->json(['errors' => 'Sorry, something went wrong, please try again later.']);
        }

        // Grab the data from the command (Command handler sets it)
        echo $command->id;

        // I want to use the ID of the uploaded photo here
        return response()->json(['success' => 'Photo uploaded successfully']);
    }

}

在命令处理程序中:

<?php namespace App\Handlers\Commands\Photos;

use App\Commands\Photos\UploadPhotoCommand;

class UploadPhotoCommandHandler {
    public function __construct() {}

    public function handle(UploadPhotoCommand $command)
    {
        // Handle your command

        // Set the ID on the command object
        $command->id = 123;
    }
}

在命令中:

<?php namespace App\Commands\Photos;

use App\Commands\Command;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class UploadPhotoCommand extends Command {

    public $photo;

    // Declare a public property ID
    public $id;

    public function __construct(UploadedFile $photo)
    {
        $this->photo = $photo;
    }

}

如果有人知道更好的方法,请告诉我。

答案 1 :(得分:0)

最直接的方法是在你的上面有一个单独的函数调用,UploadPhotoCommand类可以完成工作,而不是在构造函数中进行。

此函数调用返回包含您想要的任何内容的响应。

即。你可以这样做:

 public function process(UploadRequest $request)
{
    try
    {
        $oPhotoUploadTask = new UploadPhotoCommand();
        $results = $oPhotoUploadTask->uploadPhoto($request->file('photo'));
    }
    catch (\Exception $e)
    {
        \Log::error($e);
        return response()->json(['errors' => 'Sorry, something went wrong, please try again later.']);
    }

    // I want to use the ID of the uploaded photo here
    return response()->json(['success' => 'Photo {$results['photo_id']} uploaded successfully']);
}