Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Laravel 11 examples
    Laravel 11 Reverb WebSocket server middleware configuration per-second rate limiting directory structure Laravel features Laravel improvements


Laravel 11 examples


Here are some examples of new features and changes in Laravel 11:

Reverb WebSocket Server

Laravel 11 introduces a new first-party WebSocket server called Reverb. Here's an example of how to use Reverb for real-time communication:

1. Install Reverb using the Artisan command:


php artisan install:broadcasting

2. Start the Reverb server:


php artisan reverb:start

3. Create a new event that implements the `ShouldBroadcast` interface:

php
class NewMessageEvent implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $message;

    public function __construct($message)
    {
        $this->message = $message;
    }

    public function broadcastOn()
    {
        return new PrivateChannel('chat-room');
    }
}

4. Dispatch the event when a new message is created:

php
event(new NewMessageEvent($newMessage));

5. In the frontend, use Laravel Echo to listen for the event:

js
window.Echo.private('chat-room')
    .listen('NewMessageEvent', (e) => {
        this.messages.push(e.message);
    });

This example demonstrates how to set up Reverb, create a custom event, and listen for the event on the frontend using Laravel Echo.

Middleware Configuration

In Laravel 11, middleware configuration has been moved to the `bootstrap/app.php` file. Here's an example of how to configure middleware:

php
return Application::configure(basePath: dirname(__DIR__))
    ->withMiddleware(function (Middleware $middleware) {
        // Apply middleware to all routes
        $middleware->use([ExampleMiddleware::class]);

        // Use it only in the web routes
        $middleware->web([ExampleMiddleware::class]);

        // API only
        $middleware->api([ExampleMiddleware::class]);
    })
    // Other configuration
    ->create();

This example shows how to register middleware for all routes, web routes, and API routes using the `withMiddleware()` method.

Per-Second Rate Limiting

Laravel 11 supports "per-second" rate limiting for HTTP requests and queued jobs. Here's an example:

php
RateLimiter::for('invoices', function (Request $request) {
    return Limit::perSecond(1);
});

This will limit the number of requests to the "invoices" route to 1 per second.

Streamlined Directory Structure

Laravel 11 has a streamlined directory structure, with the removal of the `Kernel.php` file and the `middleware` directory. Here's an example of the new structure:


app/
    Http/
        Controllers/
        Requests/
    Providers/
bootstrap/
    app.php
routes/
    web.php
    api.php
    console.php

The middleware configuration is now handled in the `bootstrap/app.php` file, and the controllers no longer extend the base `Controller` class by default.

These are just a few examples of the new features and changes in Laravel 11. The framework has undergone significant improvements to simplify the application structure, enhance performance, and provide more flexibility in configuration.

Citations:
[1] https://laravel-news.com/laravel-11
[2] https://laravel.com/docs/11.x/reverb
[3] https://laravel.com/docs/11.x/releases
[4] https://blog.laravel.com/laravel-11-now-available
[5] https://www.youtube.com/watch?v=G8xIEejDyDY