Laravel 5.7 Middleware – Create Custom Middleware with Example

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 :



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 :



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 :



Route::get('validate', '[email protected]');


Add Method in Controller

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



 namespace App\Http\Controllers;

 use Illuminate\Http\Request;
 class HomeController extends Controller
     public function validate()

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 :


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


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.


My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *