Laravel 11 Middleware Example

Laravel 11 Middleware Example

There are many major changes in Laravel 11 application, the default folder of middleware has been removed from Lara application, this is also one of them.

In this example guide, we will show you how to create, register and use custom middleware with routes, controllers, and models in laravel 11 applications.

Steps on How to Create and Use Custom Middleware in Laravel 11

Here are steps:

Step 1: Download the New Laravel Application

Run composer create-project --prefer-dist laravel/laravel myBlog command on cmd to download and setup new laravel application into your system:

composer create-project --prefer-dist laravel/laravel myBlog

Step 2 – Create Middleware

Run the php artisan make:middleware AdminMiddleware command to create a custom middleware class in application:

php artisan make:middleware AdminMiddleware

This command will create the new middleware class in your Laravel application along with the middleware folder, Which you can verify in the app/Http/middleware/ folder.

Step 3 – Add Middleware Class in App

Open app.php file from Bootstrap folder, and add your middleware class in it; something like this:

<?php
   
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
   
return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        $middleware->alias([
            'AdminMiddleware' => \App\Http\Middleware\AdminMiddleware::class,
        ]);
    })
    ->withExceptions(function (Exceptions $exceptions) {
        //
    })->create();

Step 4 – Add Bussiness Logic Into Middleware File

Now open AdminMiddleware.php file from app/Http/middleware/ folder and add some logic in it; something like this:

<?php

namespace App\Http\Middleware;

use Closure;

class AdminMiddleware
{
    public function handle($request, Closure $next)
    {
        if(auth()->check() && auth()->user()->isAdmin()) {
            return $next($request);
        }

        abort(403, 'Unauthorized action.');
    }
}

Step 5 – Use Middleware in Routes

Open web.php file from routes folder, and use middleware with routes in it; something like this:

Route::middleware(['AdminMiddleware'])->group(function () {
    Route::get('admin/dashboard', function () {
        return 'Welcome to the admin dashboard!';
    });
});

Conclusion

We have done that by helping you learn how to create and use middleware in a Laravel 11 application with the help of this guide.

Recommended Guides

AuthorAdmin

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 *