Home » Php » Get Header Authorization key in laravel controller?

Get Header Authorization key in laravel controller?

Posted by: admin November 30, 2017 Leave a comment

Questions:

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?

Answers:

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