Writing custom middleware
Before Flare receives the data that was collected from your local exception, we give you the ability to call custom middleware methods. These methods retrieve the report that should be sent to Flare and allow you to add custom information to that report.
Just like with the Flare client itself, you can add custom context information to your report as well. This allows you to structure your code so that you have all context related changes in one place.
You can register a custom middleware by using the registerMiddleware
method on the Facade\FlareClient\Flare
class, like this:
use Spatie\FlareClient\Report;
use Spatie\LaravelFlare\Facades\Flare; // Replace by Spatie\LaravelIgnition\Facades\Flare when using the `spatie/laravel-ignition` package
Flare::registerMiddleware(function (Report $report, $next) {
// Add custom information to the report
$report->context('key', 'value');
return $next($report);
});
A middleware can either be a callable, as seen above, or a custom class that implements a handle
method. This class can make use of dependency injection in its constructor:
Here is an example:
use Spatie\LaravelFlare\Facades\Flare; // Replace by Spatie\LaravelIgnition\Facades\Flare when using the `spatie/laravel-ignition` package
Flare::registerMiddleware(FlareMiddleware::class);
To create a middleware that, for example, removes all the session data before your report gets sent to Flare, the middleware implementation might look like this:
use Spatie\FlareClient\Report;
class FlareMiddleware
{
public function handle(Report $report, $next)
{
$context = $report->allContext();
$context['session'] = null;
$report->userProvidedContext($context);
return $next($report);
}
}