spatie/laravel-disable-floc
Automatically disables Google’s FLoC in Laravel apps by adding a Permissions-Policy header. Installs via Composer and works out of the box—no configuration needed. Applies the header to responses that don’t already set Permissions-Policy.
Installation:
composer require spatie/laravel-disable-floc
The package auto-discovers Laravel 5.5+ and requires no manual registration.
First Use Case:
Permissions-Policy header includes interest-cohort=():
Permissions-Policy: interest-cohort=(), geolocation=()
Where to Look First:
config/floc.php (auto-generated) for customization options.Default Behavior:
interest-cohort=() into the Permissions-Policy header via Laravel’s middleware stack.Conditional Disabling:
// Disable FLoC only for specific routes
Route::middleware(['web', DisableFloc::class])->group(function () {
// Routes where FLoC is disabled
});
Dynamic Control:
public function handle(Request $request, Closure $next)
{
if ($request->user()->shouldDisableFloc()) {
app(DisableFloc::class)->disable();
}
return $next($request);
}
Integration with Existing Headers:
Permissions-Policy header without conflicts:
// In AppServiceProvider boot()
$this->app[DisableFloc::class]->addToHeader('geolocation=()');
Custom Headers:
// Disable FLoC and add CHIPs header
DisableFloc::addToHeader('interest-cohort=(), ch-ip-lookup=()');
Middleware Stacking:
Combine with other Spatie middleware (e.g., spatie/laravel-activitylog) for layered privacy controls.
Header Conflicts:
Permissions-Policy, ensure interest-cohort=() isn’t overridden. Use DisableFloc::addToHeader() to append safely.Caching Headers:
Cache-Control: no-cache to verify real-time changes.Laravel Version:
app/Http/Kernel.php:
protected $middleware = [
// ...
\Spatie\DisableFloc\Middleware\DisableFloc::class,
];
Verify Headers:
Use dd($request->headers->get('Permissions-Policy')) in a middleware to inspect the header.
Disable for Testing:
Temporarily disable the package in config/floc.php:
'enabled' => env('DISABLE_FLOC_TESTING', false),
Custom Logic: Override the default header logic by publishing the config:
php artisan vendor:publish --provider="Spatie\DisableFloc\DisableFlocServiceProvider"
Then modify config/floc.php to include custom conditions.
Event-Based Control:
Listen for floc.disabled events to react to FLoC changes:
event(new FLoCDisabled($request));
Conditional Middleware: Dynamically enable/disable via a service:
app(DisableFloc::class)->disable(); // Disable for current request
app(DisableFloc::class)->enable(); // Re-enable
How can I help you explore Laravel packages today?