Laravel:如何在重定向到任何URL时设置自定义标头 [英] Laravel: How to set custom header while redirecting to any url

查看:564
本文介绍了Laravel:如何在重定向到任何URL时设置自定义标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Laravel 5.0,并尝试使用自定义标头重定向到url.但是不知何故,我无法在重定向页面中获得标头值,或者可以说重定向时标头值未得到发送.

I am using Laravel 5.0 and trying to redirect to a url with custom headers. But somehow I am not getting the header value in redirected page or we can say header value is not getting sent while redirecting.

我正在使用以下代码:

return redirect('http://www.anydomain.com')
            ->header('customvalue1', $customvalue1)
            ->header('customvalue2', $customvalue2);

如果我做错了事,请告诉我.

Please let me know if I'm doing something wrong.

推荐答案

恐怕,其他答案完全是错误的并且具有误导性.

I'm afraid, the other answer is completely wrong and misleading.

无论使用哪种语言或框架,都无法重定向到设置了自定义标题的页面.换句话说,无法触发HTTP重定向并导致客户端(浏览器)添加自定义标头.

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.

您可能会认为此代码应该可以正常工作:

You might be thinking that this code should work just fine:

return redirect('http://www.anydomain.com')
     ->header('customvalue1', $customvalue1)
     ->header('customvalue2', $customvalue2);

但是不会.您正在为响应设置自定义标头,该标头指示浏览器重定向,而不是重定向本身.

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.

网站指示浏览器发出带有自定义标头的HTTP请求的唯一方法是使用Javascript和XMLHttpRequest对象.并且它需要在目标服务器上实现的CORS才能允许此类Ajax请求.

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.

请注意,除非页面使用XMLHttpRequest发出异步请求,否则该页面无法设置HTTP请求标头.这意味着您也无法使用客户端上的自定义标头进行此类重定向.

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.

网络不是这样运作的.

That's not how the web works.

这篇关于Laravel:如何在重定向到任何URL时设置自定义标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆