With Laravel, you may filter HTTP requests that enter your application with the use of middleware, a valuable capability. Middleware is capable of handling many tasks such as logging activity, performing authentication, and limiting access according to user roles.
Overview
Between a request and a response, middleware acts as a link. It offers a practical method for filtering Http requests in your Application , It can be used for the following :-
- Authentication
- Logging
- Caching
- Request modification
The management of middleware has undergone significant changes with Laravel 11. In Laravel 11, middleware registration is now performed within the app.php file, as opposed to previous versions when it was handled in Kernel.php.
Thus, let’s examine how to create custom middleware, how to customise Laravel 11’s default middleware, and what middleware does with Laravel 11. In Laravel 11, middleware registration is now performed within the app.php file, as opposed to previous versions when it was handled in Kernel.php. Thus, let’s examine how to create custom middleware, how to customise Laravel 11’s default middleware, and what middleware does.
Set up Laravel 11
Install composer and Run the code :
1 |
composer create-project laravel/laravel /Applications/MAMP/htdocs/test_laravel |
Develop Middleware
The following command will now be used to construct middleware.
1 |
php artisan make:middleware IsAdmin |
Open the middleware file
app/Http/Middleware/IsAdmin.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
namespace App\Http\Middleware; use Closure; use Illuminate\Http\Request; class IsAdmin { public function handle(Request $request, Closure $next) { if (auth()->user() && auth()->user()->is_admin) { return $next($request); } return redirect('home'); } } |
Register Up Middleware
This step involves registering our custom middleware in the app.php file, as shown in the following code snippet:
bootstrap/app.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?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([ 'isAdmin' => \App\Http\Middleware\IsAdmin::class, ]); }) ->withExceptions (function (Exceptions $exceptions) { // })->create(); |
Utilise Middleware
We’ll apply the “isAdmin” middleware and construct two routes in this stage. Let’s move forward and make the appropriate code : routes/web.php
1 2 3 4 5 6 7 8 |
<?php use Illuminate\Support\Facades\Route; Route::middleware (['isAdmin'])->group (function () { Route::get('/dashboard', function () { return 'Dashboard'; }); Route::get('/users', function () { return 'Users'; }); }); |
Launch the Application for Laravel 11
Run the laravel 11 application using the following command.
1 |
Php Artisan Serve |
Typical Errors and Debugging Techniques
Typical Errors :
- Class Found Error: If you receive an error message such as “Class ‘App\Http\Middleware\IsAdmin’ not found,” make sure that the directory structure and the namespace in your IsAdmin.php file match, and that the file has been included appropriately in the app.php file.
- Unregistered Middleware: Make sure the middleware is properly registered by checking the bootstrap/app.php file if your middleware isn’t functioning.
Tips for Debugging
- Logging: For debugging purposes, log messages inside your middleware using Laravel’s logging tools.
- dd() and dump(): To output the contents of a variable and halt execution for debugging, use dd($variable) or dump($variable).
conclusion
With Laravel 11, developing custom middleware is a simple procedure that can significantly improve the functionality of your application. The methods described in this article can be used to construct middleware to perform a variety of jobs, includingrequest validation, logging, and authentication. Don’t forget to consult the official Laravel documentation for more detailed instructions and recommended practices.