Laravel in Apache getting header value

前端 未结 2 765
灰色年华
灰色年华 2021-02-10 22:36

I have the following piece of code in Laravel BaseController. I want to protect all my api resources with an Authorization header with a token.

  pu         


        
相关标签:
2条回答
  • 2021-02-10 22:58

    It is a Laravel & Apache problem, this line in public/.htaccess fixed it for me:

    RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    

    The fix is from https://github.com/dingo/api/issues/54

    0 讨论(0)
  • 2021-02-10 23:07

    You cannot have two returns in PHP:

    return Request::header();
    return Response::json();
    

    So in your code, only the header will return, and your code will exit.

    I think this will work

    return Response::json(['error'=>'Not authorized. Access token needed in Header.Authorization'], 403)->header('Authorization');
    

    If not - this definetely will:

    $response = Response::json(['error'=>'Not authorized. Access token needed in Header.Authorization'], 403);
    $response->header('Authorization');
    return $response;
    
    0 讨论(0)
提交回复
热议问题