jeffersongoncalves/laravel-cookie-consent
Simple Laravel cookie consent banner with GDPR/CCPA-friendly preferences. Drop-in head/body includes, optional view publishing for customization, and settings stored in the database via spatie/laravel-settings with helper access for runtime updates.
Installation:
composer require jeffersongoncalves/laravel-cookie-consent
php artisan migrate
Include in Layout:
Add these lines to your master template (e.g., resources/views/layouts/app.blade.php):
@include('cookie-consent::cookie-consent-head')
@include('cookie-consent::cookie-consent-body')
First Use Case:
CookieConsentSettings (see below).resources/views/vendor/cookie-consent/ (after running php artisan vendor:publish --tag=cookie-consent-views)database/migrations/[timestamp]_create_cookie_consent_settings_table.phpvendor/jeffersongoncalves/cookie-consent/src/CookieConsentServiceProvider.php (for extension points)Runtime Configuration:
// In a controller or service
$settings = cookie_consent_settings();
$settings->position = 'top-right';
$settings->popup_background = '#2c3e50';
$settings->save();
CookieConsentSettings class or cookie_consent_settings() helper for type-safe access.Dynamic Theming:
css_url and js_url in settings:
$settings->css_url = asset('css/cookie-consent-custom.css');
$settings->js_url = asset('js/cookie-consent-custom.js');
Conditional Rendering:
@if(!cookie_consent()->hasConsented())
<!-- Show banner or custom fallback -->
@endif
cookie_consent() helper for consent logic.Multi-Language Support:
content_* settings (e.g., content_header, content_message).Admin Panels:
CookieConsentSettings via Filament/Nova for non-developers to manage:
// Filament example
use JeffersonGoncalves\CookieConsent\Settings\CookieConsentSettings;
public static function getSettings(): array
{
return [
CookieConsentSettings::class,
];
}
API Consent:
HasConsent trait to add API-specific consent checks:
use JeffersonGoncalves\CookieConsent\Traits\HasConsent;
class ApiUser extends Model
{
use HasConsent;
}
Third-Party Services:
// In EventServiceProvider
CookieConsent::consented(function ($user) {
event(new ConsentGranted($user));
});
Testing:
$this->actingAs($user)
->withCookie('cookie_consent', 'true')
->get('/');
Migration Pitfalls:
config/cookie-consent.php) is deprecated. All settings now live in the database.
php artisan migrate after updating to v2.0+.Caching Issues:
php artisan view:clear
CDN Dependencies:
cookieconsent.min.css/js. For offline use:
$settings->css_url = asset('vendor/cookieconsent/cookieconsent.min.css');
$settings->js_url = asset('vendor/cookieconsent/cookieconsent.min.js');
php artisan vendor:publish --tag=cookie-consent-assets
Consent Persistence:
cookie_consent). Ensure your SameSite cookie policy aligns with GDPR requirements:
// In AppServiceProvider
Cookie::queue('cookie_consent', 'true', 365 * 24 * 60);
Inspect Settings:
dd(cookie_consent_settings()->toArray());
Cookie Debugging:
Application > Cookies).$response = new Response('...');
$response->withCookie(cookie('cookie_consent', 'true', 365));
JavaScript Errors:
js_url points to a valid path. Test with:
$settings->js_url = asset('js/cookieconsent.min.js');
Custom Consent Logic:
hasConsented() method in a service:
class CustomConsentService extends CookieConsentService
{
public function hasConsented(): bool
{
return parent::hasConsented() || auth()->check();
}
}
AppServiceProvider:
$this->app->bind(CookieConsentService::class, CustomConsentService::class);
Event Hooks:
CookieConsent::consented(function ($user) {
// Sync with CRM, analytics, etc.
});
Custom Views:
php artisan vendor:publish --tag=cookie-consent-views
resources/views/vendor/cookie-consent/cookie-consent.blade.php.Localization:
content_* settings for multi-language support:
$settings->content_header = __('cookie_consent.header');
A/B Testing:
$settings->popup_background = request()->ip() % 2 === 0 ? '#2c3e50' : '#e74c3c';
Dark Mode:
$settings->popup_background = request()->user()->prefers_dark_mode ? '#1a1a1a' : '#696969';
Analytics Integration:
CookieConsent::consented(function ($user) {
Analytics::track('cookie_consent_granted', ['user_id' => $user->id]);
});
How can I help you explore Laravel packages today?