How to Add Middleware in Laravel 12

2 months ago admin Laravel

In this tutorial, we will see how to add middleware in Laravel 12, Now we can use bootstrap/app.php instead of http/kernel.php this change came with the release of Laravel 11.


Create the middleware

First, let's assume that we want to add an admin middleware to protect the admin routes.
Let's create a new middleware. I gave it 'AdminMiddleware' as a name.

                                                    
                                                                                                                
php artisan make:middleware AdminMiddleware



Register the middleware using an alias

Next, inside the file bootstrap/app.php let's register the middleware and give it 'admin' as an alias.

                                                        
                                                                                                                        
<?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',
        api: __DIR__.'/../routes/api.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        //
        $middleware->alias([
            'admin' => \App\Http\Middleware\AdminMiddleware::class,
        ]);
    })
    ->withExceptions(function (Exceptions $exceptions) {
        //
    })->create();



Check if user is an admin

Inside the 'AdminMiddleware', let's add the code to check if the logged-in user is an admin

                                                        
                                                                                                                        
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class AdminMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        if (auth()->guard('admin')->check()) {
            return $next($request);
        }
        return redirect()->back();
    }
}



Use the middleware

Now, inside the web.php file, we can use the admin middleware to protect our routes.

                                                        
                                                                                                                        
Route::middleware('admin')->get('/admin', function () {
    return view('dashboard');
});


Related Tuorials

How to Add Query Parameters to Laravel Pagination

In this lesson, we will see how to add query parameters to Laravel pagination.Our goal in this lesso...


How to Add Query Parameters to Laravel Pagination with Vue 3 and Inertia.js

In this lesson, we will see how to add query parameters to Laravel pagination with Vue 3 and inertia...


How to Build a Shopping Cart in Laravel 12 (Step-by-Step) Part 3

In the third part of this tutorial, we will display the products on the home page and add, update, a...


How to Build a Shopping Cart in Laravel 12 (Step-by-Step) Part 2

In the second part of this tutorial, we will add the controllers we need with the function...


How to Build a Shopping Cart in Laravel 12 (Step-by-Step) Part 1

In this tutorial, we will Learn how to create a fully functional shopping cart in Laravel 12. The us...


How to Add a Relationship to a Select in Filament

In this lesson, we will see how to add a relationship to a select in Filament.Let's assume that we h...


How to Generate Slugs from a Title in Filament

In this lesson, we will see how to generate slugs from a title in Filament, Sometimes we need to gen...


How to Change the Order of the Filament Left Navigation Menu Items

In this lesson, we will see how to change the order of the filament left navigation menu items.Somet...


How to Hide the Info Widget on the Filament Dashboard

In this lesson, we will see how to hide the info widget on the Filament dashboard. The info widget i...


How to Add Bootstrap 5 to Laravel 12 with Vite

In this tutorial, we will see how to add Bootstrap 5 to Laravel 12 with Vite, The process is simple...