我可以从index.php
看到如何让$response
对象返回四个Http\Request
:
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$response = $kernel->handle(
$request = Illuminate\Http\Request::capture()
);
这几乎是我想要做的,但我需要将路线和POST参数换成新的路线。更具体地说,我有这样的事情:
class AjaxLoaderController extends Controller {
public static function endpoint(\Illuminate\Http\Request $httpRequest) {
$kernel = app()->make(Illuminate\Contracts\Http\Kernel::class);
foreach($httpRequest->request->get('requests',[]) as $subReq) {
$newReq = $httpRequest->duplicate(null,$subReq['data']);
// how can I change the URL for $newReq to the resolved path for an arbitrary route name?
$subRes = $kernel->handle($newReq);
\Kymark\Dbg::log('$subReq',$subReq);
}
}
}
$subReq
的样子:
array:1 [
0 => array:2 [
"route" => "survey"
"data" => array:1 [
"companyId" => 3
]
]
]
所以我想解决"调查"到一个URL,以便我可以将其填入$newReq
然后用{"数据"来热交换$httpRequest
中的POST数据,然后通过正常的Laravel管道将其关闭以便它调用新的控制器操作并给我一个新的响应。
答案 0 :(得分:1)
你在做什么会让你陷入无限循环:(
因为你没有改变路线。
如果我想这样做,我将创建一个新请求,然后将数据添加到其中
你应该在Illuminate之前添加\ ......
$kernel = app()->make(\Illuminate\Contracts\Http\Kernel::class);
... 在你的循环中:
//$newReq = $request->duplicate(null,$subReq);
$newReq = Request::create($subReq['route'], 'POST', $subReq['data']);
$subRes = $kernel->handle($newReq);
$subRes->send();
你可以从以下地方除外:
app/Http/Middleware/VerifyCsrfToken.php
$subReq = [
"route" => "survey",
"data" => [
"companyId" => 3,
'_token' => csrf_token(), // if you dont want to except csrf
],
];
$server = $request->server();
$server['REQUEST_METHOD'] = 'POST'; // add it if you are initially sending a GET,
$server['REQUEST_URI'] = $subReq['route'];
$newRequest = $request->duplicate(null, $subReq['data'], null, null, null, $server);
$newRequest->initialize([], $subReq['data'], [], $request->cookie(), [], $server, null);
app()->instance('request', $newRequest);
\Route::dispatch($newRequest);
// or you can still do this
// $kernel = app()->make(\Illuminate\Contracts\Http\Kernel::class);
// $subRes = $kernel->handle($newRequest);
// $subRes->send();
$kernel = app()->make(\Illuminate\Contracts\Http\Kernel::class);
$subRes = $kernel->handle($newRequest);
$subRes->send();