Exploring Middleware Updates in Laravel 11
Welcome, Laravel developers!
In this blog post, we’ll dive into the significant changes introduced to middleware in Laravel 11.
Goodbye Kernel.php
, Hello bootstrap/app.php
With Laravel 11, middleware registration moves to the bootstrap/app.php
file. This file acts as the central configuration hub for various application aspects, including middleware.
Create middleware
php artisan make:middleware EnsureTokenIsValid
Add middleware inside bootstrap/app.php
Inside this file you will see,withMiddleware
closure and it is responsible for managing the middleware assigned to your application’s routes.
<?php
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use App\Http\Middleware\EnsureTokenIsValid;
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->append(EnsureTokenIsValid::class);
})
->withExceptions(function (Exceptions $exceptions) {
//
})->create();
The append
method adds the middleware to the end of the list of global middleware. If you would like to add a middleware to the beginning of the list, you should use the prepend
method.
Global moddleware
The Global Middleware will run on every HTTP request of the application, whereas the Route Middleware will be assigned to a specific route.
Laravel includes predefined web
and api
middleware groups that contain common middleware you may want to apply to your web and API routes.
Laravel automatically applies these middleware groups to the corresponding routes/web.php
and routes/api.php
files.
If you want to customize these middleware check out official documentation.
Middleware Alias
You can add aliases to middleware, Like this.
use App\Http\Middleware\EnsureUserIsSubscribed;
->withMiddleware(function (Middleware $middleware) {
$middleware->alias([
'subscribed' => EnsureUserIsSubscribed::class
]);
})
It allows you to define a short alias for a given middleware class. This middleware will not be added to your global middleware list untill you add it.
Route::get('/profile', function () {
// ...
})->middleware('subscribed');
If you would like to assign middleware to specific routes, You can do like this.
use App\Http\Middleware\EnsureTokenIsValid;
Route::get('/profile', function () {
// ...
})->middleware(EnsureTokenIsValid::class);
Controller middleware
You may find it convenient to specify middleware within your controller class. To do so, our controller should implement the HasMiddleware
interface, which dictates that the controller should have a static middleware
method.
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Routing\Controllers\HasMiddleware;
use Illuminate\Routing\Controllers\Middleware;
class UserController extends Controller implements HasMiddleware
{
/**
* Get the middleware that should be assigned to the controller.
*/
public static function middleware(): array
{
return [
'auth',
new Middleware('log', only: ['index']),
new Middleware('subscribed', except: ['store']),
];
}
// ...
}
Remember to explore the official Laravel documentation for more details on these updates and how they impact your application. Happy coding! 🚀