2

I'm encountering a problem where a redirect from one route to another is calling the targeted controller method twice. This question addresses a similar issue, but the OP passing a 301 status code was deemed to be the issue in the accepted answer, and I'm not specifying any status code. I'm also using the session state for parameters. The relevant code looks something like this:

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

Any ideas what could be causing this? I don't have any next middleware or any of the other possible causes that are suggested in related questions where controllers are executed twice.

Thanks for any help!

Eli S
  • 63
  • 1
  • 6
  • Not really familiar with Laravel, but have you tried putting a breakpoint into the method to look into what calls it the second time? (Or, alternatively, using `debug_print_backtrace()`?) – Jeto Aug 01 '20 at 20:49
  • @Jeto Thanks for the comment. Looking through the stack, it's just a bunch of Laravel framework functions, nothing coming from my own code. – Eli S Aug 01 '20 at 20:58

1 Answers1

2

Have you tried passing values in parameters? Try the below code.

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'));
}
Sohail Ahmed
  • 1,308
  • 15
  • 17
  • Thanks for the response! There was a reason that I wanted to use session parameters instead, but using normal request parameters seems to prevent the extra redirect call. Any ideas why using session would cause this? – Eli S Aug 01 '20 at 20:59
  • According to official documention, redirecting to Controller Actions. Parameters are recommended. https://laravel.com/docs/5.6/redirects#redirecting-controller-actions – Sohail Ahmed Aug 01 '20 at 21:12