What’s New in Laravel 11

What’s New in Laravel 11

Laravel 11 is the latest major release of the popular PHP web application framework, packed with exciting new features and improvements. In this article, we’ll explore the highlights and how to take advantage of them.

Streamlined Application Structure

One of the most significant changes in Laravel 11 is the streamlined application structure, designed to provide a leaner and more modern experience.

The Application Bootstrap File (bootstrap/app.php)

The revamped bootstrap/app.php file now serves as a centralized, code-first configuration file for your application. Here's how to utilize 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',
)        

3. Configure your middleware stack:

->withMiddleware(function (Middleware $middleware) {
    $middleware->validateCsrfTokens(
        except: ['stripe/*']
    );
})        

4. Customize exception handling:

->withExceptions(function (Exceptions $exceptions) {
    $exceptions->dontReport(MissedFlightException::class);
})        

Service Providers and Middleware

Laravel 11 ships with fewer default service providers and middleware files. The functionality has been incorporated into the bootstrap/app.php file.

Opt-in API and Broadcast Routing

The api.php and channels.php route files are no longer present by default. You can create them using Artisan commands:

php artisan install:api
php artisan install:broadcasting        

Laravel Reverb

Laravel 11 introduces Laravel Reverb, a first-party, scalable WebSocket server that enables robust real-time communication in your applications.

  1. Start the Reverb server:

php artisan reverb:start        

2. Integrate Reverb with Laravel Echo for real-time client-side functionality.

3. Utilize Reverb’s features, such as broadcasting events or managing WebSocket connections, within your application’s code.

Per-Second Rate Limiting

Laravel 11 now supports per-second rate limiting, providing more granular control over resource consumption. To implement it, use 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

New Laravel 11 applications include a default health routing directive, defining a simple health-check endpoint at /up. This endpoint can be invoked by third-party application health monitoring services or orchestration systems like Kubernetes.

Graceful Encryption Key Rotation

Laravel 11 introduces graceful encryption key rotation, allowing you to define previous encryption keys via the APP_PREVIOUS_KEYS environment variable. This feature ensures that users can continue using your application uninterrupted, even if your encryption key is rotated.

Prompt Validation

Laravel 11 integrates with the Laravel Prompts package, enabling beautiful and user-friendly command-line form validation. You can now utilize Laravel’s validator when validating prompt inputs:

$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),
    ];
}        

The once Helper Function

The once helper function executes the given callback and caches the result in memory for the duration of the request, preventing redundant execution:

function random(): int
{
    return once(function () {
        return random_int(1, 1000);
    });
}        

Improved Performance for In-Memory Database Testing

Laravel 11 offers a significant speed boost when using the :memory: SQLite database during testing by maintaining a reference to PHP's PDO object and reusing it across connections.

Improved Support for MariaDB

Laravel 11 includes a dedicated MariaDB driver, providing better defaults and optimizations for this database system.

Inspecting Databases and Improved Schema Operations

Laravel 11 provides additional database schema operation and inspection methods, including native modifying, renaming, and dropping of columns, as well as advanced spatial types and non-default schema names.

These are just some of the exciting new features and improvements introduced in Laravel 11. Be sure to review the official Laravel documentation and upgrade guide for more detailed information and instructions on upgrading your existing applications.

To view or add a comment, sign in

Insights from the community

Others also viewed

Explore topics