laravel 使用 Request::create 和 Route::dispatch($request);

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32847926/
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-09-14 12:25:15  来源:igfitidea点击:

laravel use Request::create and Route::dispatch($request);

phplaravellaravel-4

提问by Yotav Masa

This is my code

这是我的代码

$request = Request::create('games/result', 'POST', array(
                 "name"     => Session::get('name'),
                 "score"    => Session::get('score'),
                 "Level"    => Session::get('Level'),
                 "accuracy" => Session::get('accuracy'), 
                 "time"     => Session::get('time'),
                 "bouns"    => Session::get('bouns')
            ));
            var_dump($request->input());
            Request::replace($request->input());
             Route::dispatch($request);

The problem is that when its go to the route the inputs are not form the array how I can get the inputs from the array

问题是,当它进入路由时,输入不形成数组我如何从数组中获取输入

回答by Ján Kyselica

Don't use Request::replace method.

不要使用 Request::replace 方法。

$request = Request::create('games/result', 'POST', array(
             "name"     => Session::get('name'),
             "score"    => Session::get('score'),
             "Level"    => Session::get('Level'),
             "accuracy" => Session::get('accuracy'), 
             "time"     => Session::get('time'),
             "bouns"    => Session::get('bouns')
        ));           
$response = Route::dispatch($request);
return $response;

回答by Néstor

I know this question has more than 1 year but for people who arrives here trying to find answers to a similar issue for Laravel 4.2, before to create the request add: Input::setMethod('post');and after create the request: Request::replace($request->input());The first one forces to use POST method and the second one replaces the current request with the new one created or instanced with the $requestvariable. I think is a bug in this Laravel version.

我知道这个问题已经超过 1 年了,但是对于到达这里试图找到 Laravel 4.2 类似问题答案的人来说,在创建请求之前添加:Input::setMethod('post');和创建请求之后:Request::replace($request->input());第一个强制使用 POST 方法和第二个用$request变量创建或实例化的新请求替换当前请求。我认为是这个 Laravel 版本中的一个错误。

回答by Jay

This works on Laravel 5.4 - and maybe others too.

这适用于 Laravel 5.4 - 也许其他人也适用。

In my web controller:

在我的网络控制器中:

public function testground(Request $request)
{   

    $new_request = Request::create(
        'api/customsandbox', 
        'POST', 
        [$request->all(), ['param' => 2]]
    );

    $response = Route::dispatch($new_request);
    dd($response);
}

By doing it this way, I didn't need to set the method type (GET, POST, etc) after. I can just set it in the Request::create().

通过这样做,我不需要在之后设置方法类型(GET、POST 等)。我可以在 Request::create() 中设置它。

My api route looks like this (have a versioning folder added):

我的 api 路由如下所示(添加了一个版本控制文件夹):

Route::post('customsandbox', 'Api\V1\SandboxController@index');

Finally my Api's SandboxController index method looks like this:

最后,我的 Api 的 SandboxController 索引方法如下所示:

public function index(Request $request)
{
    return response()->json($request->all());
}

That flattens the array and gives me this response:

这使数组变平并给了我这个响应:

array:2 [
  0 => array:1 [
    "param" => "1"
  ],
  1 => array:1 [
    "param" => 2
  ]
]