Laravel 8 Middleware Example Tutorial

Laravel 8 middleware example; In this tutorial, you will learn how to create and how to use middleware in laravel 8 app.

Laravel middleware filters all the HTTP requests in laravel based projects. For example when the user is doing any request that time middleware check user is logged in or not and redirect accordingly. Any user is not logged in but he wants to access to the dashboard or other things in projects that time middleware filter request redirects to the user.

Now, we will give an example of active or inactive users accesses laravel 8 app or not. So, add this middleware with routes to restrict logged users to access routes, if he/she is inactive by admin.

How to use middleware in Laravel 8

  • Step 1 – Create Middleware
  • Step 2 – Register Middleware
  • Step 3 – Implement Logic Into Your Middleware File
  • Step 4 – Add Route
  • Step 5 – Add Method in Controller

Step 1 – Create Middleware

In this step, open terminal and execute the following command to create custom middleware in laravel 8 app. So let’s open your command prompt and execute below command on it:

php artisan make:middleware CheckStatus

Step 2 – Register Middleware

Register middleware, so 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 = [
        ....
        'checkStatus' => \App\Http\Middleware\CheckStatus::class,
    ];
}

Step 3 – Implement Logic Into Your Middleware File

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

app/Http/Middleware/CheckStatus.php

<?php

namespace App\Http\Middleware;

use Closure;

class CheckStatus
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (auth()->user()->status == 'active') {
            return $next($request);
        }
            return response()->json('Your account is inactive');

    }
}

Step 4 – Add Route

In this step, simply create a route and use custom middleware with routes for filter every http request:

 //routes/web.php

use App\Http\Controllers\HomeController;
use App\Http\Middleware\CheckStatus;

Route::middleware([CheckStatus::class])->group(function(){

Route::get('home', [HomeController::class,'home']);

});

Step 5 – Add Method in Controller

In this step, create one method name home and add this method on HomeController.php file, which is placed on app/Http/Controllers/ directory:

<?php

 namespace App\Http\Controllers;

 use Illuminate\Http\Request;
 class HomeController extends Controller
 {
     public function home()
     {
         dd('You are active');
     }
 }
?>

Conclusion

Laravel 8 middleware tutorial, You have learned how to create custom middleware in laravel 8 based project and as well as how to use it.

Recommended Laravel Posts

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including 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. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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