我无法将请求handle()
排入队列
public function handle(Request $request) | package.json*
{ | phpunit.xml*
Log::alert('starting process'); | readme.md*
Log::alert($request); | server.php*
|~
if (strpos($request->status, 'Approved') !== false) { |~
$name = Name::where('mId', '=', $request->mId)->get()->first(); |~
|~
$client = new Client(); |~
|~
$client->request('POST', 'http://127.0.0.1:5000/api/email', [ |~
'json' => [ |~
'type' => $request->type, |~
'name' => $name->name,
] |~
]); |~
}
实际上,$request
为空。如果我删除Request
而只离开handle($request)
,则会得到以下堆栈:
Too few arguments to function App\Jobs\PostAlfred::handle(), 0 passed and exactly 1 expected in
更新表单时,我正在从控制器调用此函数。
public function update(UpdateRequest $request) |▸ vendor/
{ | artisan*
$redirect_location = parent::updateCrud($request); | composer.json* | composer.lock*
PostMyJob::dispatch($request);
我尝试添加UpdateRequest
,例如:handle(UpdateRequest $request)
,然后出现授权错误。
不确定如何进行。
答案 0 :(得分:1)
将任何参数传递给
dispatch
函数时,这些参数将在作业的constructor
中传递,而不是在handle
方法中传递。
在您的工作中执行此操作:
class SomeJob extends Job{
private $request;
public function __construct(Request $request)
{
$this->request = $request;
}
public function handle()
{
if (strpos($this->request->status, 'Approved') !== false) {
$name = Name::where('mId', '=', $this->request->mId)->get()->first();
$client = new Client();
$client->request('POST', 'http://127.0.0.1:5000/api/email', [
'json' => [
'type' => $this->request->type,
'name' => $name->name,
]
]);
}
}
}
答案 1 :(得分:0)
请记住,请求仅存在于实际HTTP请求的上下文中。它仅在您的应用正在处理该请求时存在。当您的队列工作者开始将作业从队列中取出时,没有“请求”。 Laravel无法为您提供请求的实例,因为没有实例。
您需要做的是显式传递您的工作所需的信息,以履行其职责。如果只希望输入请求,则可以执行以下操作-向工作的构造函数提供输入数组。
PostMyJob::dispatch($request->all())
public function __construct(array $input)
{
$this->input = $input;
}
您可能已经看到了Eloquent模型传递到工作中的示例,但是不要让您以为整个类都将按原样提供给处理程序。 Laravel足够聪明,可以在处理工作时为您重新获取Eloquent模型,但是如前所述,它无法为您获取原始请求。