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 Spatie\FlareClient\Flare
class, like this:
use Spatie\FlareClient\Report;
// Get access to your registered Flare client instance
$flare->registerMiddleware(function (Report $report, $next) {
// Add custom information to the report
$report->context('key', 'value');
return $next($report);
});
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);
}
}