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.
Installation:
composer require devrabiul/laravel-cookie-consent
php artisan vendor:publish --provider="Devrabiul\CookieConsent\CookieConsentServiceProvider"
This publishes the default config, views, and assets to config/cookie-consent.php, resources/views/vendor/cookie-consent/, and public/vendor/cookie-consent/.
Basic Integration: Add the package's styles and scripts to your Blade template:
<!DOCTYPE html>
<html>
<head>
{!! CookieConsent::styles() !!}
</head>
<body>
<!-- Your content -->
{!! CookieConsent::scripts() !!}
</body>
</html>
First Use Case:
Enable cookie consent for analytics and marketing in .env:
COOKIE_CONSENT_ANALYTICS=true
COOKIE_CONSENT_MARKETING=true
This will automatically show the consent banner on page load, allowing users to accept/reject cookies.
config/cookie-consent.php – Centralize all settings (cookie categories, lifetimes, themes, translations).CookieConsent::styles() and CookieConsent::scripts() – Minimal integration points.necessary, analytics, marketing) in the config.Consent Management:
CookieConsent::hasConsent('analytics') in your controllers to check if a user has consented to a category.if (CookieConsent::hasConsent('analytics')) {
Analytics::trackPageView();
}
Dynamic Script Loading:
loadGoogleAnalytics) in your assets file (e.g., public/js/cookie-consent.js).'cookie_categories' => [
'analytics' => [
'js_action' => 'loadGoogleAnalytics',
],
],
Multi-Language Support:
php artisan vendor:publish --tag=cookie-consent-lang.resources/lang/{locale}/cookie-consent.php:
return [
'Privacy Policy' => 'Datenschutzrichtlinie',
];
Theme Customization:
basic, modern-blue, trust-green, etc.) in the config:
'theme_preset' => 'trust-green',
public/css/cookie-consent.css for full control.Dark Mode:
theme="dark" to the <body> tag or setting:
'theme' => 'dark',
theme="auto".Middleware for Protected Routes: Create middleware to block analytics scripts if consent is missing:
public function handle($request, Closure $next) {
if (!$request->user() && !CookieConsent::hasConsent('analytics')) {
abort(403, 'Analytics consent required.');
}
return $next($request);
}
Event Listeners: Dispatch custom events when consent changes:
CookieConsent::consentChanged(function ($category, $accepted) {
event(new CookieConsentChanged($category, $accepted));
});
API Consent Checks: Pass consent status via API responses:
return response()->json([
'data' => $data,
'consent' => [
'analytics' => CookieConsent::hasConsent('analytics'),
'marketing' => CookieConsent::hasConsent('marketing'),
],
]);
Localization:
Use the CookieConsent::translate() helper for dynamic text:
<p>{!! CookieConsent::translate('cookie_description') !!}</p>
Asset Paths in Local Development:
COOKIE_CONSENT_ASSET_URL, set it to null in .env during local testing to avoid CORS issues:
COOKIE_CONSENT_ASSET_URL=null
Cookie Prefix Conflicts:
cookie_prefix in the config doesn’t clash with existing cookies (e.g., Laravel’s laravel_session):
'cookie_prefix' => 'app_',
JavaScript Execution Order:
js_action run after the consent modal is dismissed. Avoid relying on them for critical page functionality.RTL Layout Quirks:
bar-inline) may require manual adjustments for button alignment.Caching Issues:
php artisan view:clear
php artisan config:clear
Check Consent Status:
Inspect cookies in browser dev tools (look for {cookie_prefix}_consent).
Use Tinker to debug:
php artisan tinker
>>> \Devrabiul\CookieConsent\Facades\CookieConsent::getConsent();
Log Consent Changes: Enable debug mode in the config:
'debug' => env('COOKIE_CONSENT_DEBUG', false),
This logs consent events to storage/logs/cookie-consent.log.
Verify Script Loading: Check the browser console for errors after dismissing the modal. Common issues:
js_action functions.COOKIE_CONSENT_ASSET_URL misconfigured).Custom Modal Templates:
Override the default Blade template in resources/views/vendor/cookie-consent/modal.blade.php:
@extends('vendor.cookie-consent::modal')
@section('custom_content')
<p>Your custom content here.</p>
@endsection
Dynamic Cookie Categories: Fetch categories from a database:
'cookie_categories' => CookieCategory::all()->pluck('name')->map(function ($name) {
return [
'enabled' => true,
'locked' => false,
'title' => __("CookieConsent::{$name}_title"),
];
})->toArray(),
Event-Driven Extensions: Listen for consent changes to trigger custom logic:
CookieConsent::consentChanged(function ($category, $accepted) {
if ($accepted && $category === 'analytics') {
Analytics::flushEvents();
}
});
Headless Mode: Disable the UI but keep consent logic:
'consent_modal_enabled' => false,
'consent_banner_enabled' => false,
Useful for APIs or non-interactive contexts.
A/B Testing:
Randomize the consent_modal_layout in the config to test different designs:
'consent_modal_layout' => ['box-wide', 'bar-inline'][rand(0, 1)],
Conditional Consent: Disable consent for specific routes:
Route::middleware(['cookie-consent:analytics'])->group(function () {
// Routes requiring analytics consent
});
Localization Fallback: Ensure default translations are published:
php artisan vendor:publish --tag=cookie-consent-lang --force
Performance:
Lazy-load scripts by setting disable_page_interaction: true to prevent resource-hogging until consent is granted.
Compliance Audits:
Use the CookieConsent::getAuditLog() method to generate compliance reports:
$log = CookieConsent::getAuditLog();
// Export to CSV or database
How can I help you explore Laravel packages today?