Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

chrislentz's avatar

How do you set a Cookie on a Redirect instead of a Response?

I am trying to figure out how I can set a cookie on a redirect instead of a response. Can anyone help me figure out how to accomplish this? The code I have now (below) causes a 502 Bad Gateway error.

Cookie::queue($name, $value);
return redirect()->route('dashboard');
0 likes
6 replies
TerrePorter's avatar

The error "502 Bad Gateway" is most often related to a bad route.

I'd verify the route named 'dashboard' is valid and works.

chrislentz's avatar

@TerrePorter The route is fine. If I remove the cookie, the route loads fine. Also, the 502 hits before the redirect. I also tried the following, but it didn't work:

$cookie = Cookie::make($name, $value);

return redirect()->route('dashboard')->withCookie($cookie);
TerrePorter's avatar

Humm..

What about trying

Redirect::route('dashboard')->withCookie($cookie);
TerrePorter's avatar

@chrislentz Ok, maybe trying with some simple data in the cookie in case it is the data being passed is causing the error.

$cookie = Cookie::make('foo', 'bar');

Another idea would be to set a cookie on another page just to make sure it is working outside of this route/controller/etc.

Does the php error log show anything, maybe it is a filesystem permission error or something ... just another idea.

Please or to participate in this conversation.