devrabiul/laravel-cookie-consent
GDPR-compliant cookie consent for Laravel with one-click install. Lightweight, no frontend deps, responsive banner with dark mode, RTL/i18n, and granular category controls (necessary/analytics/marketing). Fully customizable colors, text, and layout via config.
composer require devrabiul/laravel-cookie-consent
php artisan vendor:publish --provider="Devrabiul\CookieConsent\CookieConsentServiceProvider"
<!-- In <head> -->
{!! CookieConsent::styles() !!}
<!-- Before </body> -->
{!! CookieConsent::scripts() !!}
config/cookie-consent.php for default settings (e.g., cookie lifetimes, categories).COOKIE_CONSENT_ANALYTICS=true) to enable/disable categories.Initial Setup:
necessary, analytics, marketing) in config/cookie-consent.php.loadGoogleAnalytics) in your assets file.Dynamic Configuration:
CookieConsent::scripts():
{!! CookieConsent::scripts([
'cookie_lifetime' => 30,
'theme' => 'trust-green',
'disable_page_interaction' => false,
]) !!}
Integration with Analytics/Marketing:
js_action in cookie categories to trigger scripts (e.g., Google Analytics, Facebook Pixel) only after consent:
'analytics' => [
'js_action' => 'loadGoogleAnalytics',
'enabled' => env('COOKIE_CONSENT_ANALYTICS', false),
],
loadGoogleAnalytics() function in your JavaScript file.Localization:
CookieConsent::translate('Privacy Policy')).dir="rtl" to <body> and setting rtl_enabled: true in config.Dark Mode:
theme: 'dark' in config or adding theme="dark" to <body>.Preferences Modal:
'preferences_modal_enabled': true in config.bar or box) and content (e.g., cookie_modal_intro).disable_page_interaction: true to block page interaction until consent is given.necessary cookies ('locked': true) while allowing users to toggle others.<a onclick="showHideToggleCookiePreferencesModal()">Cookie Settings</a>
Asset Paths:
COOKIE_CONSENT_ASSET_URL is set correctly. Localhost may require null.COOKIE_CONSENT_ASSET_URL=null in .env for local development.Cookie Conflicts:
cookie_prefix in config (e.g., 'cookie_prefix': 'MyApp_').JavaScript Actions:
js_action functions (e.g., loadGoogleAnalytics) will break script loading.js_action functions in your global JS file.Caching Issues:
reject_lifetime: 0 to force re-prompting or clear cookies manually.Dark Mode Conflicts:
!important sparingly or inspect the generated classes (e.g., .cookie-consent-dark).Laravel_App_consent) or use CookieConsent::getConsent() in Laravel.COOKIE_CONSENT_LOG_EVENTS=true to debug consent changes via Laravel logs.dir="rtl" and lang="ar" attributes to verify RTL and translation support.Custom Themes:
php artisan vendor:publish --tag=cookie-consent-assets
resources/views/vendor/cookie-consent/ files.Event Listeners:
CookieConsentAccepted, CookieConsentRejected) to log or trigger actions:
// In EventServiceProvider
protected $listen = [
'Devrabiul\CookieConsent\Events\CookieConsentAccepted' => [
'App\Listeners\LogConsentEvent',
],
];
API Integration:
CookieConsent::getConsent() to fetch consent data in API routes:
$consent = CookieConsent::getConsent();
return response()->json(['analytics_enabled' => $consent->analytics]);
Multi-Tenant Support:
// In CookieConsentServiceProvider
$this->app->singleton('cookie-consent.handler', function () {
return new \App\Services\TenantCookieConsentHandler();
});
disable_page_interaction if you’re using a lightweight analytics script.theme_preset values (e.g., modern-blue, trust-green) to test user engagement.COOKIE_CONSENT_LOG_EVENTS and review logs for granular consent tracking.resources/lang/ to avoid runtime translation delays.
```markdown
## Advanced: Dynamic Configuration via Middleware
Create middleware to dynamically adjust consent settings (e.g., disable analytics for admins):
```php
// app/Http/Middleware/CookieConsentMiddleware.php
public function handle($request, Closure $next) {
if (auth()->check() && auth()->user()->isAdmin) {
config(['laravel-cookie-consent.cookie_categories.analytics.enabled' => false]);
}
return $next($request);
}
Register the middleware in app/Http/Kernel.php:
protected $middleware = [
\App\Http\Middleware\CookieConsentMiddleware::class,
];
How can I help you explore Laravel packages today?