如何从 Laravel 5 中的 HTTP 响应获取自定义标头?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41118242/
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 14:54:51  来源:igfitidea点击:

How to get custom header from HTTP response in Laravel 5?

phplaravelhttpangularlaravel-5

提问by Caius

I'm trying to access a custom header from the Request in Laravel. The header name is "accessing_from". Listing all the headers in Laravel, gives me only the "standard ones", but the one that I've set isn't present in the list. Checking in the browser network tab I can see that the header gets sent. So I'm wondering how to access it from within Laravel.

我正在尝试从 Laravel 中的请求访问自定义标头。标题名称是“accessing_from”。列出 Laravel 中的所有标题,只给我“标准标题”,但我设置的标题不在列表中。检查浏览器网络选项卡,我可以看到标题已发送。所以我想知道如何从 Laravel 中访问它。

I'm using Angular2 to make the request with the default http service.

我正在使用 Angular2 通过默认的 http 服务发出请求。

The Laravel's $response->header() dump:

Laravel 的 $response->header() 转储:

enter image description here

在此处输入图片说明

The web inspector's log:

网络检查员的日志:

enter image description hereThanks to anyone!

在此处输入图片说明感谢任何人!

采纳答案by Alexey Mezenin

Are you talking about get parameter or something? If so, use:

你说的是获取参数还是什么?如果是这样,请使用:

request()->accessing_from;

For header you should use:

对于标题,您应该使用:

request()->header('accessing_from');

The working solution for this was the answer (the last one) of daverhere: Laravel get request header

这个工作的解决方案是这里的答案(最后一个)daverLaravel get request header

回答by HARLET

Have you tried simple php?

你试过简单的php吗?

<?php
// Replace XXXXXX_XXXX with the name of the header you need in UPPERCASE
$headerStringValue = $_SERVER['HTTP_XXXXXX_XXXX'];

Full answer: https://stackoverflow.com/a/541463/3548658

完整答案:https: //stackoverflow.com/a/541463/3548658

The docs says: https://laravel.com/api/5.3/Illuminate/Http/Request.html#method_header

文档说:https: //laravel.com/api/5.3/Illuminate/Http/Request.html#method_header

use Request;
Request::header('accessing_from');

回答by Artistan

getting a custom header in Laravel 5.8.

在 Laravel 5.8 中获取自定义标头。

this should apply to earlier versions as well.

这也适用于早期版本。

If using a header like X-Requested-With: XMLHttpRequestyou may notice that it converts this to HTTP_X_REQUESTED_WITH.

如果使用像X-Requested-With: XMLHttpRequest您这样的标头,您可能会注意到它会将其转换为HTTP_X_REQUESTED_WITH.

This, in turn, is converted to lower case version for the header()method.

这反过来又被转换为该header()方法的小写版本。

request()->header('x_requested_with');

I would suggest using Accessing-From: adminwhich will add the apache header HTTP_ACCESSING_FROM. And you will be able to access it via the header function like this...

我建议使用Accessing-From: adminwhich 将添加 apache 标头HTTP_ACCESSING_FROM。你将能够通过这样的标题功能访问它......

request()->header('accessing_from');