0
0
Laravelframework~5 mins

Terminable middleware in Laravel

Choose your learning style9 modes available
Introduction

Terminable middleware lets you run code after the response is sent to the user. This helps with tasks like logging or cleanup without slowing down the user.

You want to log request details after the user sees the page.
You need to clear temporary data after sending the response.
You want to send notifications or emails after the response is done.
You want to measure how long the request took without delaying the user.
You want to perform background tasks after the main work is finished.
Syntax
Laravel
class ExampleMiddleware implements TerminableMiddlewareInterface
{
    public function handle($request, Closure $next)
    {
        // Code before response
        $response = $next($request);
        // Code after response preparation but before sending
        return $response;
    }

    public function terminate($request, $response)
    {
        // Code that runs after response is sent
    }
}

The terminate method runs after the response is sent to the browser.

Make sure your middleware is registered in the kernel.php to use terminable middleware.

Examples
This middleware logs the request path after the response is sent.
Laravel
class LogAfterResponse implements TerminableMiddlewareInterface
{
    public function handle($request, Closure $next)
    {
        return $next($request);
    }

    public function terminate($request, $response)
    {
        // Log request info after response
        Log::info('Request completed: ' . $request->path());
    }
}
This middleware deletes a temporary file after the user gets the response.
Laravel
class CleanupAfterResponse implements TerminableMiddlewareInterface
{
    public function handle($request, Closure $next)
    {
        return $next($request);
    }

    public function terminate($request, $response)
    {
        // Clear temporary files or cache
        Storage::delete('temp-file.txt');
    }
}
Sample Program

This middleware logs the URL path the user visited after the response is sent. It does not delay the page loading.

Laravel
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Middleware\TerminableMiddleware;
use Illuminate\Support\Facades\Log;

class AfterResponseLogger implements TerminableMiddleware
{
    public function handle($request, Closure $next)
    {
        // Just pass the request to next middleware or controller
        return $next($request);
    }

    public function terminate($request, $response)
    {
        // Runs after response sent
        Log::info('User visited: ' . $request->path());
    }
}

// In Kernel.php, register this middleware in $middleware array or route middleware

// When a user visits a page, the response is sent immediately.
// After that, the terminate method logs the path.
OutputSuccess
Important Notes

Terminable middleware only works if the middleware implements the terminate method.

It is useful for tasks that don't need to block the user waiting for the response.

Remember to register the middleware properly in app/Http/Kernel.php.

Summary

Terminable middleware runs code after the response is sent to the user.

Use it for logging, cleanup, or background tasks.

Implement the terminate method to use this feature.