简体   繁体   English

即时创建 Laravel 请求 object

[英]Create a Laravel Request object on the fly

I'm handling data in one controller and want to pass it further into another controller to avoid duplicate code.我正在处理一个 controller 中的数据,并希望将其进一步传递到另一个 controller 以避免重复代码。

Is there a way to set up a Request object that is needed in the other controller's store -method?有没有办法设置另一个控制器的store方法中需要的 Request object ? 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.我追踪了 Request inheritance 并来到 Symfony 的 Request object ,它有一个request属性,实际上是一个ParameterBag ,它包含一个方法add来添加带有值的参数。

I've tried the following but I'm getting null as result:我尝试了以下方法,但结果是null

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

I'm on Laravel 5.1 for this project.我正在为这个项目使用 Laravel 5.1。

You can use replace() :您可以使用replace()

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

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

dd($request->foo);

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.或者,为第二个控制器中发生的任何事情创建一个Job并删除ShouldQueue接口以使其同步运行会更有意义。

Hope this helps!希望这有帮助!

Creating a request object with $myRequest = new Request();使用$myRequest = new Request();创建一个请求对象$myRequest = new Request(); creates the object with method = 'GET' .使用method = 'GET'创建对象。 You can check your request's method with $myRequest->getMethod() .您可以使用$myRequest->getMethod()检查请求的方法。 As the request property holds data for POST requests you cannot use $myRequest->request->add() by default.由于request属性保存 POST 请求的数据,因此默认情况下您不能使用$myRequest->request->add() First you have to set the request's method to POST:首先,您必须将请求的方法设置为 POST:

$myRequest = new \Illuminate\Http\Request();
$myRequest->setMethod('POST');
$myRequest->request->add(['foo' => 'bar']);
dd($request->foo);

By the way using $myRequest->query->add() you can add data to a GET request.顺便说一下,使用$myRequest->query->add()您可以向 GET 请求添加数据。

To "avoid duplicate code" you need to abstract the common functionality into a dedicated class, give it a proper mnemonic name, write a set of unit tests around it, and then mock it in controllers when unittesting controllers.为了“避免重复代码”,您需要将通用功能抽象到一个专用类中,给它一个合适的助记符名称,围绕它编写一组单元测试,然后在对控制器进行单元测试时在控制器中模拟它。

but if you still need to make requests:但如果您仍然需要提出请求:

use Illuminate\Http\Request;

$request = new Request([
        'name'   => 'unit test',
        'number' => 123,
    ]);

and if you need the full functionality of the Request, you need to add some extra lines如果您需要请求的全部功能,则需要添加一些额外的行

$request
    ->setContainer(app())
    ->setRedirector(app(\Illuminate\Routing\Redirector::class))
    ->validateResolved();

您可以克隆现有请求并用新数据填充它:

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

You can add the request parameter on the fly using these methods.您可以使用这些方法动态添加请求参数。

Replace替换

replace function doc 替换函数文档

If you are in controller then, pass Request object in paramter of the function like如果您在控制器中,则在函数的参数中传递请求对象,例如

  function createUser(Illuminate\Http\Request $request){
     $request->replace(array_merge(array("new_key1"=>"new_value1","new_key_n"=>"new_value_n"), $request->all()));
}

Merge function合并功能

merge function doc 合并函数文档

function createUser(Illuminate\Http\Request $request){
     $request->merge(array("new_key1"=>"new_value1","new_key_n"=>"new_value_n"));
}

add function添加功能

 function createUser(Illuminate\Http\Request $request){
     $request->request->add(array_merge(array("new_key1"=>"new_value1","new_key_n"=>"new_value_n"), $request->all()));
}

Note:: in all function we are extending the request, mean previous parameter will remain there.注意:在我们扩展请求的所有函数中,意味着先前的参数将保留在那里。 You will be adding your own.您将添加自己的。 You can replace them all.你可以全部替换它们。

You have to change你必须改变

this这个

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

to

$myRequest = new Request();
$myRequest->merge(['foo' => 'bar']);
echo $myRequest->input('foo');

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

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