我试图使用命令,因此我可以将它们排队,因为大量请求需要30-90秒才能完成。唯一的问题是命令没有像我希望的那样将数据返回给我的控制器,相反,我得到的只是一个方便的(讽刺)" null"。这是我的文件中的一些代码片段,感谢您的帮助!
的HomeController
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();
}
}
任何帮助将不胜感激!谢谢!
答案 0 :(得分:1)
这是正确的。
如果您queue
命令 - 那么当您运行它时,不会立即发生任何事情,因此它返回null。同时,该命令将在队列处理系统的后台单独运行。
如果您需要立即回复 - 那么只需运行没有队列的命令。