Laravel 11: What's New
The well-liked PHP web application framework has released a new major version, Laravel 11, which is brimming with intriguing new features and enhancements. We'll look at the highlights and how to use them in this article.
Reduced Application Architecture
The redesigned application structure in Laravel 11 is one of the biggest updates, designed to give users a more modern and lighter experience
The Application Bootstrap File (bootstrap/app.php)
Your application's centralized, code-first configuration file is now the redesigned bootstrap/app.php file. This is how to make use of it:
1.Open your application’s bootstrap/app.php file.
2. Customize routing by specifying the web, console, and health routes:
->withRouting(
web: __DIR__.'/../routes/web.php',
commands: __DIR__.'/../routes/console.php',
health: '/up',
)
- Configure your middleware stack:
->withMiddleware(function (Middleware $middleware) {
$middleware->validateCsrfTokens(
except: ['stripe/*']
);
})
- Customize exception handling:
->withExceptions(function (Exceptions $exceptions) {
$exceptions->dontReport(MissedFlightException::class);
})
Service Providers and Middleware
There are fewer middleware files and default service providers included with Laravel 11. The bootstrap/app.php file now contains the functionality.
Opt-in API and Broadcast Routing
The route files channels.php and api.php are no longer automatically present. With the use of Artisan commands, you may make them:
php artisan install:api
php artisan install:broadcasting
Laravel Reverb
Laravel Reverb, a first-party scalable WebSocket server that lets you have reliable real-time communication in your apps, is released with Laravel 11.
- Start the Reverb server:
php artisan reverb:start
- Integrate Reverb with Laravel Echo for real-time client-side functionality.
- Utilize Reverb’s features, such as broadcasting events or managing WebSocket connections, within your application’s code.
Per-Second Rate Limiting
Per-second rate restriction is now supported by Laravel 11, giving users more precise control over how much resource they use. To put it into practice, apply the perSecond method:
RateLimiter::for('invoices', function (Request $request) {
return Limit::perSecond(1);
});
This example limits the “invoices” key to one request per second.
Health Routing
A default health routing directive that defines a basic health-check endpoint at /up is included in new Laravel 11 apps. Applications like Kubernetes or third-party application health monitoring services can call this endpoint.
Graceful Encryption Key Rotation
With the APP_PREVIOUS_KEYS environment variable, you can declare prior encryption keys thanks to Laravel 11's graceful encryption key rotation feature. This functionality makes sure that even if your encryption key is rotated, users may keep using your application without any interruptions.
Prompt Validation
With the integration of Laravel 11 and the Laravel Prompts package, command-line form validation becomes elegant and simple to use. It is now possible to validate prompt inputs using Laravel's validator:
$name = text('What is your name?', validate: [
'name' => 'required|min:3|max:255',
]);
Queue Interaction Testing
Testing queue interactions, such as asserting that a job was released, deleted, or manually failed, is now easier with the withFakeQueueInteractions method:
use App\Jobs\ProcessPodcast;
$job = (new ProcessPodcast)->withFakeQueueInteractions();
$job->handle();
$job->assertReleased(delay: 30);
New Artisan Commands
Laravel 11 introduces new Artisan commands for quickly creating classes, enums, interfaces, and traits:
php artisan make:class
php artisan make:enum
php artisan make:interface
php artisan make:trait
Model Casts Improvements
Laravel 11 supports defining your model’s casts using a method instead of a property, allowing for streamlined, fluent cast definitions:
protected function casts(): array
{
return [
'options' => AsCollection::using(OptionCollection::class),
];
}