laravel middleware check custom header for REST API

Today, We want to share with you laravel middleware check custom header for REST API.In this post we will show you laravel set request header, hear for how to get header value in laravel middleware we will give you demo and example for implement.In this post, we will learn about laravel check if header is present with an example.

laravel middleware check custom header for REST API

There are the Following The simple About Set header in request (middleware) Full Information With Example and source code.

As I will cover this Post with live Working example to develop laravel middleware check header, so the laravel set header middleware is used for this example is following below.

How to create and check custom header with middleware for REST API in Laravel?

Laravel is a web application framework with expressive, elegant syntax.The PHP Framework for Web Artisans,freeing you to create without sweating the small things. CRUD Operation With Server Side.

Your Jquery Ajax or AngularJS/Vuejs Http Request

         type: 'POST',  
         dataType: 'json',  
         url: '',  
         headers: { 'X-infinityknow': '982560624'},  
         data: {'email':'[email protected]','password':'982560624'}  
        alert('Sign In Successfully, Good Luck');  
    }).fail(function(jqXHR, ajaxOptions, thrownError){  

fire this command and create middleware.

php artisan make:middleware isCheckActiveUser


namespace App\Http\Middleware;  
use Closure;  
use Illuminate\Contracts\Auth\Guard;  
use Response;  
class isCheckActiveUser  
    public function handle($request, Closure $next)  
            return Response::json(array('error'=-->'Infor for Please set custom users header'));  
        if($_SERVER['HTTP_X_INFINITY'] != '982560624'){  
            return Response::json(array('error'=>'Sorry dear wrong custom users header'));  
        return $next($request);  

add in app/Http/Kernel.php

namespace App\Http;  
use Illuminate\Foundation\Http\Kernel as HttpKernel;  
class Kernel extends HttpKernel  
     * The application's global HTTP middleware stack. 
     * @var array 
    protected $middleware = [  
     * The application's route middleware. 
     * @var array 
    protected $routeMiddleware = [  
        'auth' =--> \App\Http\Middleware\Authenticate::class,  
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,  
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,  
        'isCheckActiveUser' => \App\Http\Middleware\isCheckActiveUser::class,  

use in your route

Route::post('api/login', array('uses' => 'APIAuthController@login','middleware' => ['isCheckActiveUser']));
Web Programming Tutorials Example with Demo

Read :


You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about laravel auth:api middleware.
I would like to have feedback on my blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment