Get Header Authorization key in laravel controller?

If you use a specific package like "JWT" or "sanctum" you can use their own middleware to retrieve user information.

Also, Laravel provides many ways to get the authorization key like :

  • $request->bearerToken(); to get only token without 'Bearer' word the result will be like 44|9rJp2TWvTTpWy535S1Rq2DF0AEmYbEotwydkYCZ3.
  • $request->header('Authorization'); to get the full key like Bearer 44|9rJp2TWvTTpWy535S1Rq2DF0AEmYbEotwydkYCZ3

P.S. you can use request() shortcut instead of using $request variable


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
}

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


Though it's an old topic, it might be useful for somebody...
In new Laravel versions, it's possible to get bearer Authorization token directly by calling Illuminate\Http\Request's bearerToken() method:

Auth::viaRequest('costom-token', function (Request $request) {
    $token = $request->bearerToken();
    // ...
});

Or directly from a controller:

public function index(Request $request) {
    Log::info($request->bearerToken());
    // ...
}