Create Custom Middleware in Laravel 11 Easily

Diego Cortés
Diego Cortés
January 22, 2025
Create Custom Middleware in Laravel 11 Easily

Laravel 11 has brought a series of improvements and new features, including greater flexibility in creating custom middleware. This component is essential for handling HTTP requests and applying specific logic before they reach the controllers. In this article, we will explain step by step how to create custom middleware in Laravel 11 in an easy way.

What is Middleware in Laravel?

Middleware acts as a filter that intercepts requests before they reach the controllers. This is useful for implementing functionalities such as authentication, authorization, user role verification, and much more. Laravel already includes several middleware by default, but often it is necessary to create one that fits the specific needs of your application.

Creating Custom Middleware

To start creating your middleware, open your terminal and navigate to your Laravel project folder. Then, use the following command to generate a new middleware:

php artisan make:middleware MiddlewareName

Replace "MiddlewareName" with the name you want to give to your middleware. This command will create a file in the app/Http/Middleware folder with the basic structure of a middleware.

Defining Middleware Logic

Once the middleware is created, open the generated file. You will see a class that extends Handle with a handle method. This is where you can define the logic you want to implement. For example, if you want to check if a user is authenticated, you can add the following code:

public function handle($request, Closure $next)
{
    if (!auth()->check()) {
        return redirect('login');
    }

    return $next($request);
}

In this example, the middleware checks if the user is authenticated. If not, it redirects to the login page. If the user is authenticated, the request continues to the next middleware or the controller.

Registering Middleware

After defining the logic in your middleware, you need to register it in your application. To do this, open the file app/Http/Kernel.php. Within this file, you will find two properties: $middleware and $routeMiddleware.

If you want your middleware to apply to all routes, add it to the $middleware property. On the other hand, if you want to apply the middleware only to specific routes, add it to $routeMiddleware with an alias:

protected $routeMiddleware = [
    // other middleware...
    'name' => \App\Http\Middleware\MiddlewareName::class,
];

Using the Middleware in Routes

Once the middleware is registered, you can apply it to your routes. For example:

Route::get('/dashboard', [DashboardController::class, 'index'])->middleware('name');

With this, you ensure that the /dashboard route is only accessible to authenticated users.

Testing Your Middleware

To ensure that your middleware works correctly, run your application and try to access the protected route. If everything is configured correctly, the system should redirect you to the login page if you are not authenticated.

Conclusion

Creating custom middleware in Laravel 11 is a straightforward process that can help protect and optimize your application. With the steps mentioned, you can effectively implement the logic you need.

If you are interested in more news and tutorials about Laravel and other web development topics, I invite you to keep exploring my blog. There is always something new to learn!

Article information

Published: January 22, 2025
Category: Laravel Tutorials
Reading time: 5-8 minutes
Difficulty: Intermediate

Key tips

1

Take your time to understand each concept before moving on to the next one.

2

Practice the examples in your own development environment for better understanding.

3

Don't hesitate to review the additional resources mentioned in the article.

Diego Cortés
Diego Cortés
Full Stack Developer, SEO Specialist with Expertise in Laravel & Vue.js and 3D Generalist

Frequently Asked Questions

Categories

Page loaded in 26.91 ms