Laravel 5.4:控制器方法在重定向时被调用两次
Laravel 5.4: controller method is called twice on a redirect to it
我遇到了一个问题,从一个路由到另一个路由的重定向调用了目标控制器方法两次。 addresses a similar issue, but the OP passing a 301 status code was deemed to be the issue in the ,我没有指定任何状态代码。我还使用会话状态作为参数。相关代码如下所示:
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'));
}
我遇到了一个问题,从一个路由到另一个路由的重定向调用了目标控制器方法两次。
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'));
}