Laravel in Apache getting header value

前端 未结 2 761
灰色年华
灰色年华 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 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;
    

提交回复
热议问题