Laravel 11 is released : Everything you need to know
Wait is over folks, Finally Laravel 11 is released, bringing with it a host of new features and enhancements that promise to streamline your development workflow and elevate the performance of your applications.
What’s New with Laravel 11?
Laravel 11 builds upon the enhancements introduced in Laravel 10.x. It features a simplified application structure, per-second rate limiting, health routing, seamless encryption key rotation, upgraded queue testing functionalities, Resend mail transport, integration with Prompt validator, and introduces new Artisan commands, among other improvements. Additionally, Laravel Reverb, a scalable WebSocket server, is now available, offering powerful real-time capabilities for your applications.
Let’s talk about each of above mentioned features in detail:
Streamlined Application Structure
Laravel 11 introduces a streamlined application structure for new Laravel applications, without requiring any changes to existing applications. The new application structure is intended to provide a leaner, more modern experience.
The Application Bootstrap File : The bootstrap/app.php
file has been revitalized as a code-first application configuration file. From this file, you may now customize your application's routing, middleware, service providers, exception handling, and more.
Service Providers : Instead of the default Laravel application structure containing five service providers, Laravel 11 only includes a single AppServiceProvider
. The functionality of the previous service providers has been incorporated into the bootstrap/app.php
, is handled automatically by the framework, or may be placed in your application's AppServiceProvider
.
Opt-in API and Broadcast Routing : The api.php
and channels.php
route files are no longer present by default, as many applications do not require these files. Instead, they may be created using simple Artisan commands:
php artisan install:api
php artisan install:broadcasting
Middleware : Previously, Laravel applications included nine middleware. In Laravel 11, these middleware have been moved into the framework itself, so that they do not add bulk to your application’s structure. New methods for customizing the behavior of these middleware have been added to the framework and may be invoked from your application’s bootstrap/app.php
file:
->withMiddleware(function (Middleware $middleware) {
$middleware->validateCsrfTokens(
except: ['stripe/*']
);
$middleware->web(append: [
EnsureUserIsSubscribed::class,
])
})
Scheduling : Using a new Schedule
facade, scheduled tasks may now be defined directly in your application's routes/console.php
file, eliminating the need for a separate console "kernel" class:
use Illuminate\Support\Facades\Schedule;
Schedule::command('emails:send')->daily();
Exception Handling : Exception handling can now be customized from your application’s bootstrap/app.php
file instead of a separate exception handler class, reducing the overall number of files included in a new Laravel application:
->withExceptions(function (Exceptions $exceptions) {
$exceptions->dontReport(MissedFlightException::class);
$exceptions->report(function (InvalidOrderException $e) {
// ...
});
})
Application Defaults : By default, new Laravel applications use SQLite for database storage, as well as the database
driver for Laravel's session, cache, and queue. This allows you to begin building your application immediately after creating a new Laravel application, without being required to install additional software or create additional database migrations.
Laravel Reverb
Laravel Reverb brings blazing-fast and scalable real-time WebSocket communication directly to your Laravel application, and provides seamless integration with Laravel’s existing suite of event broadcasting tools, such as Laravel Echo.
php artisan reverb:start
Per-Second Rate Limiting
Laravel now supports “per-second” rate limiting for all rate limiters, including those for HTTP requests and queued jobs. Previously, Laravel’s rate limiters were limited to “per-minute” granularity:
RateLimiter::for('invoices', function (Request $request) {
return Limit::perSecond(1);
});
Graceful Encryption Key Rotation
In previous Laravel installations, rotating your application’s encryption key would log all users out of your application. In addition, decrypting data that was encrypted by the previous encryption key becomes impossible.
Laravel 11 allows you to define your application’s previous encryption keys as a comma-delimited list via the APP_PREVIOUS_KEYS
environment variable.
When encrypting values, Laravel will always use the “current” encryption key, which is within the APP_KEY
environment variable. When decrypting values, Laravel will first try the current key. If decryption fails using the current key, Laravel will try all previous keys until one of the keys is able to decrypt the value.
This approach to graceful decryption allows users to keep using your application uninterrupted even if your encryption key is rotated.
Health Routing
New Laravel 11 applications include a health
routing directive, which instructs Laravel to define a simple health-check endpoint that may be invoked by third-party application health monitoring services or orchestration systems like Kubernetes. By default, this route is served at /up
:
->withRouting(
web: __DIR__.'/../routes/web.php',
commands: __DIR__.'/../routes/console.php',
health: '/up',
)
When HTTP requests are made to this route, Laravel will also dispatch a DiagnosingHealth
event, allowing you to perform additional health checks that are relevant to your application.
Queue Interaction Testing
Previously, attempting to test that a queued job was released, deleted, or manually failed was cumbersome and required the definition of custom queue fakes and stubs. However, in Laravel 11, you may easily test for these queue interactions using the withFakeQueueInteractions
method:
use App\Jobs\ProcessPodcast;
$job = (new ProcessPodcast)->withFakeQueueInteractions();
$job->handle();
$job->assertReleased(delay: 30);
The once
Function
The once
helper function executes the given callback and caches the result in memory for the duration of the request. Any subsequent calls to the once
function with the same callback will return the previously cached result:
function random(): int
{
return once(function () {
return random_int(1, 1000);
});
}
random(); // 123
random(); // 123 (cached result)
random(); // 123 (cached result)
New Artisan Commands
New Artisan commands have been added to allow the quick creation of classes, enums, interfaces, and traits:
php artisan make:class
php artisan make:enum
php artisan make:interface
php artisan make:trait
Inspecting Databases and Improved Schema Operations
Laravel 11 provides additional database schema operation and inspection methods, including the native modifying, renaming, and dropping of columns. Furthermore, advanced spatial types, non-default schema names, and native schema methods for manipulating tables, views, columns, indexes, and foreign keys are provided:
use Illuminate\Support\Facades\Schema;
$tables = Schema::getTables();
$views = Schema::getViews();
$columns = Schema::getColumns('users');
$indexes = Schema::getIndexes('users');
$foreignKeys = Schema::getForeignKeys('users');
THE END
These newly added features in Laravel 11 are expected to significantly enhance the overall application development experience and performance.
Please free to share your thoughts and let me know which of the above discussed features you found most appealing. (Personally, I’m most excited about the introduction of Laravel Reverb, the scalable WebSocket server. Real-time capabilities can greatly enhance the interactivity and responsiveness of web applications, opening up new possibilities for engaging user experiences)
As always, If you like this article make sure to clapp-clapp 👏 and follow me on Medium for more such articles. Suggestions in the comments are always welcome :)
Since content generation is not an easy process, I wouldn’t mind if you supported me by gifting me a Ko-fi to motivate and boost my confidence :)