spatie/laravel-cookie-consent
Add a simple, customizable cookie consent banner to Laravel. Shows on first visit, stores consent, then stays hidden. No “decline” option, no tracker blocking, and no consent categories—use other tools if you need advanced compliance features.
Installation:
composer require spatie/laravel-cookie-consent
The package auto-registers. No additional steps required unless customization is needed.
First Use Case:
Add the consent banner to your layout (e.g., resources/views/layouts/app.blade.php):
@include('cookie-consent::index')
This renders a Tailwind-styled banner at the bottom of the page.
Verify Functionality:
laravel_cookie_consent) is set for 20 years (configurable).Publish the config file (optional, defaults are sufficient for most cases):
php artisan vendor:publish --provider="Spatie\CookieConsent\CookieConsentServiceProvider" --tag="cookie-consent-config"
Modify config/cookie-consent.php to adjust:
enabled (toggle globally)cookie_name (customize cookie name)cookie_lifetime (e.g., 7 for 7 days)| Task | Command/Action |
|---|---|
| Install package | composer require spatie/laravel-cookie-consent |
| Add banner to layout | @include('cookie-consent::index') |
| Customize text/translations | Publish lang files (--tag="cookie-consent-translations") |
| Disable for specific routes | Use middleware exclusion (see below) |
app.blade.php).@include('cookie-consent::index')
resources/views/vendor/cookie-consent/dialogContents.blade.php) for full control.CookieConsentMiddleware to auto-insert the banner before the closing </body> tag.bootstrap/app.php:
->withMiddleware(function (Middleware $middleware) {
$middleware->append(\Spatie\CookieConsent\CookieConsentMiddleware::class);
})
app/Http/Kernel.php:
protected $middleware = [
// ...
\Spatie\CookieConsent\CookieConsentMiddleware::class,
];
protected $routeMiddleware = [
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'cookie.consent.exclude' => \Spatie\CookieConsent\CookieConsentMiddleware::class,
];
Route::middleware(['auth', 'cookie.consent.exclude'])->group(function () {
// Admin routes
});
@if (!cookie()->has('laravel_cookie_consent'))
@include('cookie-consent::index')
@endif
Extend the package to handle multi-category consent (e.g., "Necessary" vs. "Analytics"):
// app/Services/CookieConsentService.php
use Spatie\CookieConsent\CookieConsent;
class CookieConsentService extends CookieConsent {
public function setConsent($categories = []) {
$cookieValue = json_encode($categories);
cookie()->queue($this->getCookieName(), $cookieValue, $this->getCookieLifetime());
}
}
// app/Http/Middleware/CookieConsentMiddleware.php
public function handle($request, Closure $next) {
if (!$request->hasCookie($this->cookieName)) {
// Render custom view with category options
}
return $next($request);
}
php artisan vendor:publish --provider="Spatie\CookieConsent\CookieConsentServiceProvider" --tag="cookie-consent-translations"
es):
// resources/lang/vendor/cookie-consent/es/texts.php
return [
'message' => 'Este sitio web utiliza cookies.',
'agree' => 'Aceptar cookies',
];
/* resources/css/cookie-consent.css */
.js-cookie-consent {
@apply bg-blue-900 text-white p-4 rounded-lg shadow-lg;
}
.js-cookie-consent {
@apply fixed bottom-4 right-4 max-w-md;
}
// Example: Vue 3 + Laravel
app.component('CookieConsent', {
template: '<div>@include("cookie-consent::index")</div>'
});
<div x-data="{ show: !cookie.has('laravel_cookie_consent') }" x-show="show">
@include('cookie-consent::index')
</div>
// app/Providers/Filament/AdminPanelProvider.php
public function panel(Panel $panel): Panel {
return $panel
->middleware([
'web',
\Spatie\CookieConsent\CookieConsentMiddleware::class,
])
->exceptMiddleware([
\Spatie\CookieConsent\CookieConsentMiddleware::class,
]);
}
use Spatie\CookieConsent\CookieConsent;
public function test_consent_cookie_is_set() {
$consent = new CookieConsent();
$response = $this->get('/');
$response->assertSee('Allow cookies');
$this->assertTrue($response->getCookie('laravel_cookie_consent'));
}
public function test_consent_banner_disappears_after_accept() {
$this->get('/')
->assertSee('Please be informed that this site uses cookies');
$this->post('/cookie-consent', ['accept' => true]);
$this->get('/')->assertDontSee('Please be informed');
}
SESSION_DOMAIN.SESSION_DOMAIN in .env:
SESSION_DOMAIN=.yourdomain.com
dd(request()->getCookieJar()->get('laravel_cookie_consent'));
Route::middleware(['auth', 'cookie.consent.exclude'])->group(function () {
// Admin routes
});
cookie_lifetime is reasonable (e.g., 7 days for testing).@if (!cookie()->has('laravel_cookie_consent'))
@include('cookie-consent::index')
@endif
dd(request()->cookie('laravel_cookie_consent'));
Application > Cookies).How can I help you explore Laravel packages today?