Get Header Authorization key in laravel controller?

Sunil picture Sunil · May 3, 2016 · Viewed 84.6k times · Source

Trying to get the header authorization key in controller for making an API. Request is making from fiddler.

$headers = apache_request_headers();

And the $header contains an array.

Array
(
    [User-Agent] => Fiddler
    [Host] => localhost:8000
    [Content-Length] => 102
    [Authorization] => TestKey
)

If am trying like this to fetch the Authorization , its throwing error.

$header['Authorization]

Error :

Undefined index: Authorization

Tried many ways to get the authorization, but not working anything. Is there any way to fetch this?

Answer

Szenis picture Szenis · May 3, 2016

To get headers from the request you should use the Request class

public function yourControllerFunction(\Illuminate\Http\Request $request)
{
    $header = $request->header('Authorization');

    // do some stuff
}

https://laravel.com/api/5.0/Illuminate/Http/Request.html#method_header