Laravel 5.7 Middleware – Create Custom Middleware with Example
Laravel 17-Sep-2020

Laravel 5.7 Middleware – Create Custom Middleware with Example

In this laravel middleware tutorial, we will learn how to create custom middleware and how it use in laravel based project. Simply create one custom middleware and check language in query string.

And this example also work with laravel 5.8 version.

Simply laravel middleware filter all the http request in laravel based projects. For example when user is do any request that time middleware check user is loggedin or not and redirect accordingly. Any user is not loggedin but he want to access to dashboard or other things in projects that time middleware filter request redirect to user.

Let’s start

Create Middleware

In this step, We have to create custom middleware in laravel 5.7 based project. So let’s open your command prompt and run below command :

php artisan make:middleware CheckLang

After successfully create middleware, go to app/http/kernel.php and register your custom middleware here :

app/Http/Kernel.php

<?php 
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
    .... 
    /**
     * The application's route middleware.
     *
     * These middleware may be assigned to groups or used individually.
     *
     * @var array
     */
    protected $routeMiddleware = [
        ....
        'checkLang' => \App\Http\Middleware\CheckLang::class,
    ];
}

After successfully register your middleware in laravel project, go to app/http/middleware and implement your logic here :

app/Http/Middleware/CheckLang.php

<?php
namespace App\Http\Middleware;
use Closure;
class CheckLang
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($request->lang == 'en' || $request->lang == 'ar') {
            return $next($request);
        }
            return response()->json('Not Allow');

    }
}

Add Route

In this step, simply we will create a route and use custom middleware here. Filter http every request and protect routes :

 
 //routes/web.php 
 
 Route::middleware(['checkLang'])->group(function(){
 
 Route::get('validate', 'HomeController@validate');
 
 }); 

Add Method in Controller

Now we will create one method name language and let’s check :

app/Http/Controllers/HomeController.php
<?php
 
 namespace App\Http\Controllers;
 
 use Illuminate\Http\Request;
 class HomeController extends Controller
 {
     public function validate()
     {
         dd('Allow');
     }
 }

Start Development Server

We need to start development server. Use the php artisan serve command and start your server :

 php artisan serve
 If you want to run the project diffrent port so use this below command 
 php artisan serve --port=8080 

Now hit the below route in browser :

http://localhost:8000/validate?lang='en'

We will pass lang=”en” or lang=”ar” in query string, our created custom middlware checked, if exist en or ar in query string that time it will redirect to controller method otherwise it disaplay “Not Allow” message

Conclusion

In this laravel 5.7 tutorial for custom middleware, We have successfully create custom middleware in laravel based project with simple example. our examples run quickly.