Laravel 5.4:控制器方法在重定向到它时被调用两次



我遇到一个问题,从一个路由重定向到另一个路由会调用目标控制器方法两次。这个问题解决了一个类似的问题,但通过301状态代码的OP被认为是公认答案中的问题,我没有指定任何状态代码。我还使用会话状态作为参数。相关代码如下所示:

public function origin(Request $request) {
// Assume I have set variables $user and $cvId
return redirect()
->action('SampleController@confirmUser')
->with([
'cvId' => $cvId,
'userId' => $user->id,
]);
}
public function confirmUser(Request $request) {
$cvId = session()->get('cvId');
$userId = session()->get('userId');
if (is_null($cvId) || is_null($userId)) {
// This is reached on the second time this is called, as 
// the session variables aren't set the second time
return redirect('/home');
}
// We only see the view for fractions of a second before we are redirected home
return view('sample.confirmUser', compact('user', 'cvId'));
}

你知道是什么原因造成的吗?我没有任何next中间件,也没有在控制器执行两次的相关问题中提出的任何其他可能的原因。

谢谢你的帮助!

您尝试过在参数中传递值吗?请尝试以下代码。

public function origin(Request $request) {
// Assume I have set variables $user and $cvId
return redirect()->action(
'SampleController@confirmUser', ['cvId' => $cvId, 'userId'=>$user->id]
);
}
public function confirmUser(Request $request) {
$cvId = $request->cvId;
$userId = $request->userId;
if (is_null($cvId) || is_null($userId)) {
// This is reached on the second time this is called, as 
// the session variables aren't set the second time
return redirect('/home');
}
// We only see the view for fractions of a second before we are redirected home
return view('sample.confirmUser', compact('user', 'cvId'));
}

相关内容

  • 没有找到相关文章

最新更新