gpt4 book ai didi

php - Laravel 5.4 : controller method is called twice on a redirect to it

转载 作者:行者123 更新时间:2023-12-04 09:16:03 27 4
gpt4 key购买 nike

我遇到一个问题,从一个路由重定向到另一个路由两次调用目标 Controller 方法。 This question解决了类似的问题,但传递 301 状态代码的 OP 被认为是 accepted answer 中的问题,并且我没有指定任何状态代码。我也在使用 session 状态作为参数。相关代码如下所示:

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中间件或在 Controller 执行两次的相关问题中建议的任何其他可能原因。
谢谢你的帮助!

最佳答案

您是否尝试过在参数中传递值?试试下面的代码。

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'));
}

关于php - Laravel 5.4 : controller method is called twice on a redirect to it,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63209842/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com