php 动态创建 Laravel Request 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40262947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:08:36  来源:igfitidea点击:

Create a Laravel Request object on the fly

phprequestlaravel-5.1

提问by Ben Fransen

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

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

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 requestproperty that is in fact a ParameterBagthat holds a method addto add parameters with values to it.

有没有办法设置另一个控制器的store-method 中需要的 Request 对象?我已经追踪了 Request 继承,然后来到了 Symfony 的 Request 对象,它有一个request属性,实际上是 a ParameterBag,它包含一个方法add来向它添加带有值的参数。

I've tried the following but I'm getting nullas 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。

回答by Rwd

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 Jobfor whatever is going on in your second controller and remove the ShouldQueueinterface to make it run synchronously.

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

Hope this helps!

希望这可以帮助!

回答by andras bujna

Creating a request object with $myRequest = new Request();creates the object with method = 'GET'. You can check your request's method with $myRequest->getMethod(). As the requestproperty holds data for POST requests you cannot use $myRequest->request->add()by default. First you have to set the request's method to POST:

创建一个请求对象用$myRequest = new Request();创建对象method = 'GET'。您可以使用$myRequest->getMethod(). 由于该request属性包含 POST 请求的数据,因此您不能$myRequest->request->add()默认使用。首先,您必须将请求的方法设置为 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 请求中。

回答by Yevgeniy Afanasyev

To "avoid duplicate code you" 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();

回答by RyanT

You can clone the existing request and fill it with new data:

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

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

回答by Hafiz Shehbaz Ali

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.

注意:在我们扩展请求的所有函数中,意味着先前的参数将保留在那里。您将添加自己的。你可以全部替换它们。