Inside this article we will see the concept i.e Laravel 9 How To Get Request Headers Example Tutorial. Article contains the classified information about How to get request header in laravel.
If you are looking for a solution i.e How to get headers data from request of laravel then this article will help you a lot for this. Tutorial is super easy to understand and implement it in your code as well.
A request header is an HTTP header that can be used in an HTTP request to provide information about the request context, so that the server can tailor the response. For example, the Accept-* headers indicate the allowed and preferred formats of the response.
Read More: Laravel 9 How To Get Database Name Example Tutorial
This article is not version specific, you can use use it with Laravel v7, v8, v9.
Let’s get started
Laravel Installation
Open terminal and run this command to create a laravel project.
composer create-project laravel/laravel myblog
It will create a project folder with name myblog inside your local system.
To start the development server of laravel –
php artisan serve
URL: http://127.0.0.1:8000
Assuming laravel already installed inside your system.
Create Controller – Get Request Headers
Back to project terminal and run this command to create a controller file.
$ php artisan make:controller SiteController
Above command will create a file i.e SiteController.php inside /app/Http/Controllers folder.
Read More: Laravel 9 How To Check Database Connection Tutorial
Open SiteController.php and write this code into it.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class SiteController extends Controller { public function index(Request $request) { /* Getting All Headers from request */ $headers = $request->header(); /* Getting Singe Header value from request */ //$header = $request->header('Accept'); return response()->json($headers); } }
Add Route
Open web.php from /routes folder. Add this route into it.
//... use App\Http\Controllers\SiteController; Route::get('getdata', [SiteController::class, 'index']); //...
Application Testing
Run this command into project terminal to start development server,
php artisan serve
Request details
URL: http://127.0.0.1:8000/getdata
Method: GET
To check we use Postman:
Get a specific header information:
$header = $request->header('Accept');
Output
"application/json"
We hope this article helped you to learn Laravel 9 How To Get Request Headers Example Tutorial in a very detailed way.
Read More: Laravel 9 Call Artisan Command From Closure Routes
Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.