动态创建Laravel Request对象 [英] Create a Laravel Request object on the fly

查看:164
本文介绍了动态创建Laravel Request对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个控制器中处理数据,并希望将其进一步传递到另一个控制器中以避免重复代码.

I'm handling data in one controller and want to pass it further into another controller to avoid duplicate code.

是否有一种方法可以设置另一个控制器的 store -方法中所需的Request对象?我找到了Request继承关系,并找到了Symfony的Request对象,该对象具有一个 request 属性,该属性实际上是一个 ParameterBag ,其中包含方法 add 向其中添加带有值的参数.

Is there a way to set up a Request object that is needed in the other controller's store-method? I've traced down the Request inheritance and came to Symfony's Request object which has a request property that is in fact a ParameterBag that holds a method add to add parameters with values to it.

我尝试了以下操作,但结果为 null :

I've tried the following but I'm getting null as result:

$myRequest = new Request();
$myRequest->request->add(['foo' => 'bar']);
var_dump($myRequest->foo);

我正在为此项目使用Laravel 5.1.

I'm on Laravel 5.1 for this project.

推荐答案

您可以使用 replace():

$request = new \Illuminate\Http\Request();

$request->replace(['foo' => 'bar']);

dd($request->foo);

或者,对于第二个控制器中发生的任何事情,创建一个 Job 并删除 ShouldQueue 接口以使其同步运行会更有意义.

Alternatively, it would make more sense to create a Job for whatever is going on in your second controller and remove the ShouldQueue interface to make it run synchronously.

希望这会有所帮助!

这篇关于动态创建Laravel Request对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆