devrabiul/laravel-cookie-consent
GDPR-compliant cookie consent for Laravel with one-click setup and no frontend dependencies. Fully customizable banners with RTL/i18n, dark mode, responsive UI, and granular category controls (necessary/analytics/marketing) for enterprise-grade compliance.
composer require devrabiul/laravel-cookie-consent
php artisan vendor:publish --provider="Devrabiul\CookieConsent\CookieConsentServiceProvider"
<head> and before </body>:
{!! CookieConsent::styles() !!}
<!-- Your content -->
{!! CookieConsent::scripts() !!}
Deploy a GDPR-compliant cookie banner with default settings. Test by:
Configuration:
Customize config/cookie-consent.php for:
necessary, analytics).Dynamic Loading:
Use CookieConsent::scripts(options: [...]) to pass runtime overrides (e.g., disable page interaction):
{!! CookieConsent::scripts([
'disable_page_interaction' => false,
'theme' => 'trust-green'
]) !!}
Category-Based Consent:
Define JavaScript actions per category (e.g., js_action: 'loadGoogleAnalytics' for analytics cookies). Implement these in your global JS:
function loadGoogleAnalytics() { /* GA script */ }
Multi-Language Support:
Override translations in resources/lang/{locale}/cookie-consent.php:
return [
'Privacy Policy' => 'Datenschutzrichtlinie',
];
Dark Mode/RTL:
Add theme="dark" or dir="rtl" to <body> for automatic adaptation.
box, bar, cloud, etc.) via consent_modal_layout.'preferences_modal_enabled' => true for granular user control.<a onclick="showHideToggleCookiePreferencesModal()">Update Preferences</a>
php artisan vendor:publish --tag=cookie-consent-views
Cookie Prefix Conflicts:
Ensure cookie_prefix (e.g., Laravel_App) doesn’t clash with existing cookies. Default is Laravel_CookieConsent.
JavaScript Actions:
Forgetting to implement js_action functions (e.g., loadGoogleAnalytics) will break dependent scripts. Test in console:
loadGoogleAnalytics(); // Should load GA script
Cached Assets: Clear view/compiled assets after theme/config changes:
php artisan view:clear
php artisan cache:clear
Localhost Asset Paths:
Set COOKIE_CONSENT_ASSET_URL=null in .env for localhost to avoid CORS issues.
Dark Mode Detection:
Auto-detection may fail if <body> lacks theme="dark". Fallback to manual override:
{!! CookieConsent::scripts(['theme' => 'dark']) !!}
Check Consent Status:
Inspect cookies (Laravel_CookieConsent_necessary, Laravel_CookieConsent_analytics) or use:
$consent = CookieConsent::getConsent();
dd($consent); // Returns array of user choices
Event Listeners: Listen for consent changes via events:
// In EventServiceProvider
protected $listen = [
'Devrabiul\CookieConsent\Events\ConsentChanged' => [
'App\Listeners\LogConsentChange',
],
];
Custom Themes:
Extend the theme_preset system by publishing assets:
php artisan vendor:publish --tag=cookie-consent-public
Then override resources/css/cookie-consent.css.
Additional Categories:
Add new categories in cookie_categories config and implement corresponding JS actions.
API Integration: Fetch consent status via API:
route('cookie-consent.status'); // Add to routes/web.php
Return JSON response with current consent data.
Third-Party Scripts:
Use the js_action system to load scripts conditionally:
function loadHotjar() { /* Hotjar script */ }
Then map it in config:
'hotjar' => ['js_action' => 'loadHotjar', ...],
consent_modal_layout to test different banner styles without code changes..env for toggling categories:
COOKIE_CONSENT_ANALYTICS=false # Disable in staging
disable_page_interaction: true until consent is given.theme_preset colors.How can I help you explore Laravel packages today?