繁体   English   中英

Laravel-将请求传递到队列作业

[英]Laravel - Passing Request to Queue Job

我无法将请求传递到队列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) ,然后出现授权错误。

不知道如何进行。

当您将任何参数传递给dispatch函数时,这些参数将在作业的constructor函数中传递,而不是在handle方法中传递。

参见文档The arguments passed to the dispatch method will be given to the job's constructor

在您的工作中执行以下操作:

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,
             ]
         ]);
     }
  }
}

请记住,请求仅存在于实际HTTP请求的上下文中。 它仅在您的应用正在处理该请求时存在。 当您的队列工作者开始将作业从队列中取出时,没有“请求”。 Laravel无法为您提供请求的实例,因为没有实例。

您需要做的是显式传递您的工作所需的信息,以履行其职责。 如果只需要输入请求,则可以执行类似的操作-这将为作业的构造函数提供输入数组。

PostMyJob::dispatch($request->all())
public function __construct(array $input)
{
    $this->input = $input;
}

您可能已经看到了Eloquent模型传递到工作中的示例,但不要让您以为整个类都将按原样提供给处理程序。 Laravel足够聪明,可以在处理工作时为您重新获取Eloquent模型,但是如前所述,它无法为您获取原始请求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM