Laravel

时间:2018-04-26 12:42:26

标签: php laravel-5

为什么laravel不接受来自请求标题的带下划线的变量?

我创建了一个带有变量的简单请求示例:“token_auth”,其值为123,但在我的路径中没有得到此值。

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;


class Test extends Controller
{

    public function teste(Request $request){

        dd($request->header());

    }

}

仅打印没有下划线的标题:

array:9 [
  "thisheadernounderline" => array:1 [
    0 => "312321"
  ]
  "cache-control" => array:1 [
    0 => "no-cache"
  ]
  "postman-token" => array:1 [
    0 => "3c461fd1-5bea-4100-9926-81c14cb5810c"
  ]
  "user-agent" => array:1 [
    0 => "PostmanRuntime/7.1.1"
  ]
  "accept" => array:1 [
    0 => "*/*"
  ]
  "host" => array:1 [
    0 => "localhost"
  ]
  "cookie" => array:1 [
    0 => "XSRF-TOKEN=eyJpdiI6IkJwM3pjVkFBb2hxS2d4MDFcL2srM0h3PT0iLCJ2YWx1ZSI6IiszRzhoTzV0VzN5YUkydUNUTGR5aENVd291ZW01SkZ4V2ZxQkNDTGJwbDlyMFFJZGxzNnorMkF0VUNTbHpoRndLV3FmbndJWFhkXC9cL3IzOGZvN25zN3c9PSIsIm1hYyI6IjQwZWQ1YmJhM2VjM2I3N2RiNWZlYjcwYjZmYzQ0NDk5YjkwZDc4YzRjNGQwZjQxNDVkOGU1NDU0MTA0OWI2YWYifQ%3D%3D; laravel_session=eyJpdiI6IitSckpmOFI1TmpuXC9SSUt2QVY3VlFRPT0iLCJ2YWx1ZSI6IlwvVk1EaDdYdDNxRTZLNytRcnZDTlNiaVlFTWVRVmNUOHlyVnFia0pDeE9HNWpNa3QrWlBsNnNoVEduVkhrMUhkYURoNDI4cW9RdXVHU0lIS0JZN2REQT09IiwibWFjIjoiNWJmYmJmNTdmMzJkZjQ1OGQ4NTM1NjhhMzQxNDk5NWUxOTA5OGVjOThkODkyNDgwZTA2NzEyYjFlZmE2YjVjOSJ9"
  ]
  "accept-encoding" => array:1 [
    0 => "gzip, deflate"
  ]
  "connection" => array:1 [
    0 => "keep-alive"
  ]
]

有解决方法吗?

我无法改变这个,因为这个变量来自一个API(要求改变,但他们不想改变)。

已经尝试使用laravel 5.4和laravel 5.6。

ps:简单的php工作正常(没有laravel框架)

1 个答案:

答案 0 :(得分:1)

这适合我的情况。
我在Laravel中使用了这种解决方法来解决我的问题:

foreach (getallheaders() as $name => $value) {
    echo "$name: $value\n";
}

输出:

token_auth: 123
cache-control: no-cache
Postman-Token: f5bf7878-1f64-4ee8-907a-991d73ab8667
User-Agent: PostmanRuntime/7.1.1
Accept: */*
Host: localhost
cookie: XSRF-TOKEN=eyJpdiI6IkJwM3pjVkFBb2hxS2d4MDFcL2srM0h3PT0iLCJ2YWx1ZSI6IiszRzhoTzV0VzN5YUkydUNUTGR5aENVd291ZW01SkZ4V2ZxQkNDTGJwbDlyMFFJZGxzNnorMkF0VUNTbHpoRndLV3FmbndJWFhkXC9cL3IzOGZvN25zN3c9PSIsIm1hYyI6IjQwZWQ1YmJhM2VjM2I3N2RiNWZlYjcwYjZmYzQ0NDk5YjkwZDc4YzRjNGQwZjQxNDVkOGU1NDU0MTA0OWI2YWYifQ%3D%3D; laravel_session=eyJpdiI6IitSckpmOFI1TmpuXC9SSUt2QVY3VlFRPT0iLCJ2YWx1ZSI6IlwvVk1EaDdYdDNxRTZLNytRcnZDTlNiaVlFTWVRVmNUOHlyVnFia0pDeE9HNWpNa3QrWlBsNnNoVEduVkhrMUhkYURoNDI4cW9RdXVHU0lIS0JZN2REQT09IiwibWFjIjoiNWJmYmJmNTdmMzJkZjQ1OGQ4NTM1NjhhMzQxNDk5NWUxOTA5OGVjOThkODkyNDgwZTA2NzEyYjFlZmE2YjVjOSJ9
accept-encoding: gzip, deflate
Connection: keep-alive