使用 Laravel 5 发送重定向和设置 cookie

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30550789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:40:26  来源:igfitidea点击:

send redirect and setting cookie, using laravel 5

phplaravelredirecthttpresponse

提问by Salar

I have written this code, that sets a cookie in client's browser, and after that must redirect the client to 'home' route,

我写了这段代码,它在客户端的浏览器中设置了一个 cookie,然后必须将客户端重定向到“home”路由,

$response = new Response();
$response->withCookie(cookie()->forever('language', $language));
$response->header('Location' , url('/home')) ;
return $response ;

the client receives these headers but the client doesn't make request for the "home" route enter image description here

客户端收到这些标头,但客户端不请求“home”路由 在此处输入图片说明

how should I do both, setting the cookie and redirect the user?

我应该怎么做,设置 cookie 和重定向用户?

回答by Ali Gajani

Why don't you do return Redirect::to('home');

你为什么不做 return Redirect::to('home');

Of course you can use chaining to do more things, both in L4 and L5.

当然,您可以使用链接来做更多的事情,无论是在 L4 还是 L5。

L4: return Redirect::to('home')->withCookie($cookie);

L4: return Redirect::to('home')->withCookie($cookie);

L5: return redirect('home')->withCookie($cookie);

L5: return redirect('home')->withCookie($cookie);