首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >大容量传递请求排队作业

大容量传递请求排队作业
EN

Stack Overflow用户
提问于 2019-02-20 02:31:03
回答 2查看 11.1K关注 0票数 1

我无法传递用于队列handle()作业的请求

代码语言:javascript
复制
 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

在表单更新时,我将从控制器调用此函数。

代码语言:javascript
复制
 public function update(UpdateRequest $request)                                                                                                                   |▸ vendor/
 {                                                                                                                                                                |  artisan*
     $redirect_location = parent::updateCrud($request);                                                                                                           |  composer.json*                                                                               |  composer.lock*
     PostMyJob::dispatch($request);

我尝试添加UpdateRequest,例如:handle(UpdateRequest $request),然后得到一个授权错误。

不知道该怎么做。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-02-20 10:53:25

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

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

在你的工作中这样做:

代码语言:javascript
复制
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,
             ]
         ]);
     }
  }
}
票数 3
EN

Stack Overflow用户

发布于 2019-02-20 04:22:27

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

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

代码语言:javascript
复制
PostMyJob::dispatch($request->all())
代码语言:javascript
复制
public function __construct(array $input)
{
    $this->input = $input;
}

您可能已经看到了将雄辩的模型传递到工作中的例子,但不要让它愚弄您以为整个类都会按原样提供给处理程序。Laravel很聪明,当它处理工作时,可以为您重新获取雄辩的模型,但正如前面所述,它无法为您获得原始的请求。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54777962

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档