Php Laravel 5.4:控制器方法在重定向到它时被调用两次
我遇到了一个问题,从一个路由到另一个路由的重定向两次调用目标控制器方法。解决了一个类似的问题,但是通过301状态代码的OP被认为是中的问题,我没有指定任何状态代码。我还使用会话状态作为参数。相关代码如下所示:Php Laravel 5.4:控制器方法在重定向到它时被调用两次,php,laravel,laravel-5,http-redirect,laravel-controller,Php,Laravel,Laravel 5,Http Redirect,Laravel Controller,我遇到了一个问题,从一个路由到另一个路由的重定向两次调用目标控制器方法。解决了一个类似的问题,但是通过301状态代码的OP被认为是中的问题,我没有指定任何状态代码。我还使用会话状态作为参数。相关代码如下所示: public function origin(Request $request) { // Assume I have set variables $user and $cvId return redirect() ->action('SampleController
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'));
}
不太熟悉Laravel,但是您是否尝试过在该方法中设置断点以查看第二次调用它的内容?(或者,使用
debug\u print\u backtrace()
?)@Jeto感谢您的评论。纵观堆栈,它只是一堆Laravel框架函数,没有任何来自我自己的代码。感谢您的回复!我之所以想改用会话参数是有原因的,但是使用普通的请求参数似乎可以阻止额外的重定向调用。你知道为什么使用会话会导致这种情况吗?根据官方文件,重定向到控制器操作。建议使用参数。