没有缓存头的 Laravel 重定向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15371943/
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
Laravel redirect with no cache header
提问by John Mellor
I'm using Laravel 3 and it's not obvious how to set headers in any way other than through Response::make()
.
我正在使用 Laravel 3,除了 through 之外,如何以任何方式设置标题并不明显Response::make()
。
I am doing a redirect like this:
我正在做这样的重定向:
return Redirect::to('admin/check');
I'd like to set an additional no-cache
header for the redirect like so:
我想no-cache
为重定向设置一个额外的标头,如下所示:
"Cache-Control: no-store, no-cache, must-revalidate"
I realize I could just do this directly in PHP, but is there any way to set response headers via Laravel?
我意识到我可以直接在 PHP 中执行此操作,但是有没有办法通过 Laravel 设置响应标头?
回答by vFragosop
When you call Redirect::to()
Laravel instantiates a Response object with 302 status and a Location header. That Response object is then returned by the controller and rendered as a proper HTTP response, so, at controller time, you can still change its headers.
当您调用Redirect::to()
Laravel 时,会实例化一个具有 302 状态和位置标头的 Response 对象。该 Response 对象然后由控制器返回并呈现为正确的 HTTP 响应,因此,在控制器时,您仍然可以更改其标头。
To be even more precise class Redirect extends Response
. Take a look hereYou can achieve that by simply using:
更准确地说class Redirect extends Response
。看看这里你可以通过简单地使用来实现:
return Redirect::to('admin/check')
->header('Cache-Control', 'no-store, no-cache, must-revalidate');
回答by sepehr
I'm afraid, the accepted answer is wrong and misleading!
恐怕,已接受的答案是错误的且具有误导性!
It's impossible to redirect to a page with custom headers set, no matter what language or framework you use. In other words, there's no way to trigger an HTTP redirect and cause the client (browser) to add a custom header.
无论您使用何种语言或框架,都不可能重定向到设置了自定义标头的页面。换句话说,无法触发 HTTP 重定向并导致客户端(浏览器)添加自定义标头。
You might be thinking that this code should work just fine:
您可能会认为这段代码应该可以正常工作:
return Redirect::to('admin/check')
->header('Cache-Control', 'no-store, no-cache, must-revalidate');
But it won't. You're setting the custom headers for the response which is instructing the browser to redirect, not for the redirect itself.
但不会。您正在为指示浏览器重定向的响应设置自定义标头,而不是为重定向本身设置。
The only way for a site to instruct a browser to issue an HTTP request with a custom header is to use Javascript and the XMLHttpRequest
object. And it needs CORS implemented on the target server to allow such ajax requests.
站点指示浏览器发出带有自定义标头的 HTTP 请求的唯一方法是使用 Javascript 和XMLHttpRequest
对象。并且它需要在目标服务器上实现 CORS 以允许此类 ajax 请求。
Please note that a page can not set HTTP request headers unless it's making an async request using XMLHttpRequest
. Meaning that you can't do such redirection with the custom header on the client-side as well.
请注意,页面不能设置 HTTP 请求标头,除非它使用XMLHttpRequest
. 这意味着您也无法在客户端使用自定义标头进行此类重定向。
That's not how the web works.
这不是网络的运作方式。