如何在我的 laravel 生产应用中获取 header 请求值?

How to get header request value in my laravel app on production?

在我的 laravel 应用程序请求中 header 值 returns 我本地主机上的预期值。但是当我在服务器上上传项目时,它返回空值。我创建了一个中间件 Authkey.php

public function handle(Request $request, Closure $next)
{
    $token = $request->header('API_ACCESS_KEY');
    dd($token);
    if ($token !=env('API_ACCESS_KEY')) {
           return response()->json(['message' => 'Unauthorized'],401);
    } 
    return $next($request);
}

将您请求中的 API_ACCESS_KEY 替换为 Api-Access-Key ,也在您的中间件中更新它,

因为下划线是 header 个名称的无效字符,

Translation of headers to environment variables is more strict than
 before to mitigate some possible cross-site-scripting attacks via header injection.
 Header names containing invalid characters (including underscores) are
 no longer converted to environment variables.

更多信息请查看apache new features

  1. Illuminate\Http\Request 对象
use Illuminate\Http\Request;

public function handle(Request $request, Closure $next)
{
    $headers = $request->header();
    $user_agent = $request->header('API_ACCESS_KEY');
}
  1. \请求class header() 函数
public function handle(Request $request, Closure $next)
{
    $headers = \Request::header();
    // or pass parameter to get specific header
    $user_agent = \Request::header('API_ACCESS_KEY');
}
  1. apache_request_headers() 方法
public function handle(Request $request, Closure $next)
{
    $headers = apache_request_headers();

    dd($headers);
}
  1. 全局 getallheaders() 方法
public function handle(Request $request, Closure $next)
{
    $headers = getallheaders();

    dd($headers);
}