【问题标题】:Laravel 5 Command not Returning DataLaravel 5 命令不返回数据
【发布时间】:2015-06-04 05:25:48
【问题描述】:

我正在尝试使用命令,以便我可以将它们排队,因为一堆请求需要 30-90 秒才能完成。唯一的问题是该命令没有像我希望的那样将数据返回到我的控制器,相反,我得到的只是一个方便的(讽刺)“null”。这是我文件中的一些代码 sn-ps,感谢您的帮助!

家庭控制器

class HomeController extends Controller {

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    //
}

/**
 * Show the application dashboard to the user.
 *
 * @return Response
 */
public function index()
{
    var_dump($this->dispatch(new \App\Commands\GetFeedCommand("http://alerts.weather.gov/cap/us.atom")));
}

GetFeedController

use App\Commands\Command;

use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldBeQueued;
use GuzzleHttp\Client;

class GetFeedCommand extends Command implements ShouldBeQueued {

    use InteractsWithQueue, SerializesModels;

    public $guzzle;
    public $uri;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct($uri)
    {
        $this->guzzle = new Client;
        $this->uri = $uri;
    }

}

GetFeedControllerHelper

use App\Commands\GetFeedCommand;

use Illuminate\Queue\InteractsWithQueue;

class GetFeedCommandHandler {

    /**
     * Create the command handler.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the command.
     *
     * @param  GetFeedCommand  $command
     * @return void
     */
    public function handle(GetFeedCommand $command)
    {
        return $command->guzzle->get($command->uri)->xml();
    }

}

任何帮助将不胜感激!谢谢!

【问题讨论】:

    标签: php laravel laravel-5


    【解决方案1】:

    没错。

    如果你 queue 命令 - 那么当你运行它时,不会立即发生任何事情,所以它返回 null。同时,该命令将在您的队列处理系统的后台单独运行。

    如果您需要立即回复 - 只需在没有队列的情况下运行命令。

    【讨论】:

    • 好点,我不敢相信我没有想到这一点!感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2015-07-04
    • 1970-01-01
    • 1970-01-01
    • 2020-05-04
    • 1970-01-01
    • 2019-12-06
    • 2015-08-06
    • 2015-06-30
    相关资源
    最近更新 更多