In Laravel application, there are many ways you can get request headers. We will discuss about few ways from them.
Laravel provides many details in Illuminate\Http\Request
class object. You can simply get headers details using headers() method. This will return all headers in array.
use Illuminate\Http\Request;
/**
* Show the resource list
*
* @return void
*/
public function index(Request $request)
{
$headers = $request->header();
// or pass parameter to get specific header
$user_agent = $request->header('user-agent');
}
You can use static header()
method from the \Request
class as below.
/**
* Show the resource list
*
* @return void
*/
public function index(Request $request)
{
$headers = \Request::header();
// or pass parameter to get specific header
$user_agent = \Request::header('user-agent');
}
apache_request_headers()
is PHP method that returns the header data. You can use this method to get headers.
/**
* Show the resource list
*
* @return void
*/
public function index(Request $request)
{
$headers = apache_request_headers();
dd($headers);
}
The getallheaders()
method is alias for apache_request_headers() and returns the same data as above. This will return array of headers.
/**
* Show the resource list
*
* @return void
*/
public function index(Request $request)
{
$headers = getallheaders();
dd($headers);
}
So, this is most common ways to get headers in Laravel. I hope you liked this small article.
Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]
How to install VLC media player in Windows, MacOS and Linux
VLC is a free and open source cross-plat...Start Applications Automatically on Login in Ubuntu
Many of us need to open certain applicat...The requested URL was not found on this server
After installing phpMyAdmin, when you tr...How to Get List of File In Folder In PHP
In this article, I will share with you h...How to Count Number of Rows in a Table Using jQuery
Use the length Property You can simpl...