To get the headers from a Laravel Request object, you can use the headers
method of the Request object.
Here is an example of how to get the headers from a Laravel Request object:
use Illuminate\Http\Request;
Route::get('/', function (Request $request) {
$headers = $request->headers;
foreach ($headers as $header => $value) {
echo "$header: $value<br>";
}
});
In this example, the headers
method of the Request object returns an instance of the Symfony\Component\HttpFoundation\HeaderBag
class, which implements the IteratorAggregate
interface and can be iterated over like an array.
You can also use the header()
method of the Request object to get the value of a specific header. For example:
$userAgent = $request->header('User-Agent');
echo $userAgent;
This code gets the value of the User-Agent
header from the Request object and assigns it to the $userAgent
variable.