whitecube/laravel-cookie-consent
Installation:
composer require whitecube/laravel-cookie-consent
Publish the service provider, config, and optionally views/translations:
php artisan vendor:publish --tag=laravel-cookie-consent-service-provider
php artisan vendor:publish --tag=laravel-cookie-consent-config
php artisan vendor:publish --tag=laravel-cookie-consent-views --tag=laravel-cookie-consent-lang
Register Cookies:
Override registerCookies() in app/Providers/CookiesServiceProvider.php:
protected function registerCookies(): void {
Cookies::essentials()->session()->csrf();
Cookies::analytics()->google(id: 'UA-XXXXXX');
}
Add Blade Directives: Include in your layout:
@cookieconsentscripts
@cookieconsentview
Enable GDPR-compliant cookie consent for a production site with Google Analytics:
// In CookiesServiceProvider
Cookies::analytics()
->google(id: config('services.google_analytics.id'), anonymizeIp: true);
Cookie Registration:
essentials, analytics, optional, or custom).name(), description(), and duration().google() for GA4).Consent Handling:
Cookies::consent() or dependency injection.@cookieconsentscripts to load JS only after consent.Dynamic Features:
accepted() callbacks:
Cookies::optional()
->name('theme')
->accepted(fn(Consent $consent) => $consent->script('<script src="/theme.js"></script>'));
@cookieconsentscripts
if (Cookies::consent()->has('optional')) {
$theme = session('theme');
}
domain() in accepted() to sync consent across subdomains.| Pattern | Example |
|---|---|
| Essential Cookies | Cookies::essentials()->session()->csrf() |
| Analytics | Cookies::analytics()->google(id: 'UA-123') |
| Custom Cookie | Cookies::optional()->name('prefs')->accepted(fn(Consent $c) => $c->cookie('value')) |
| Conditional JS | @cookieconsentscripts (loads only after consent) |
Missing Service Provider:
Class 'Cookies' not found.CookiesServiceProvider is registered after RouteServiceProvider in config/app.php.Consent Not Persisting:
parent::registerCookies() in custom providers.class CookiesServiceProvider extends \Whitecube\LaravelCookieConsent\CookiesServiceProvider {
protected function registerCookies(): void {
parent::registerCookies();
// Custom cookies...
}
}
GA Scripts Not Loading:
@cookieconsentscripts in <head>.Translation Keys Missing:
config/cookieconsent.php:
'categories' => [
'analytics' => [
'title' => 'Analytics Cookies',
'description' => 'Track visitor behavior.',
],
],
dd(Cookies::consent()->all()); // Returns array of consented categories
__cookie_consent are set.registerCookies() to verify cookie registration:
\Log::debug('Registered cookies:', Cookies::all());
Custom Categories:
Extend CookiesCategory for unique logic:
class MyCategory extends CookiesCategory {
public function specialCookie() {
return $this->cookie(fn(Cookie $c) => $c->name('special')->duration(30));
}
}
Register via:
Cookies::category('my-category', fn() => new MyCategory());
Override Views: Publish views and modify:
php artisan vendor:publish --tag=laravel-cookie-consent-views
Customize resources/views/vendor/cookie-consent/modal.blade.php.
Dynamic Consent UI:
Use @cookieconsentview with custom data:
@cookieconsentview(['showDetails' => true])
duration(0) for session-only cookies to reduce storage.Esc.darkmode_enabled cookie).How can I help you explore Laravel packages today?