statikbe/laravel-cookie-consent
Installation:
composer require statikbe/laravel-cookie-consent
Publish the config and views:
php artisan vendor:publish --provider="Statikbe\CookieConsent\CookieConsentServiceProvider" --tag="config"
php artisan vendor:publish --provider="Statikbe\CookieConsent\CookieConsentServiceProvider" --tag="views"
Configuration:
Edit config/cookie-consent.php to define:
required, optional, or denied).position, theme).First Use Case:
Add the consent banner to your layout (e.g., resources/views/layouts/app.blade.php):
@cookieConsent
This injects the modal and handles cookie storage via a cookie_consent session key.
User Interaction:
cookie_consent session variable (or cookie, if configured).Conditional Logic:
Use the hasConsent() helper or CookieConsent::check() to gate functionality:
@if (CookieConsent::check('analytics'))
<!-- Load Google Analytics -->
<script src="https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID"></script>
@endif
Dynamic Cookie Management:
config/cookie-consent.php:
'categories' => [
'analytics' => [
'purpose' => 'Track user behavior for analytics',
'default' => 'denied', // or 'optional', 'required'
],
'marketing' => [
'purpose' => 'Personalized ads',
'default' => 'denied',
],
],
CookieConsent::set() to override defaults programmatically:
CookieConsent::set('analytics', 'accepted');
Integration with Frontend:
document.addEventListener('cookieConsentChanged', (e) => {
console.log('Consent updated:', e.detail);
// Re-load scripts or update UI
});
@cookieConsent directive to pass custom data:
@cookieConsent(['custom' => 'value'])
API/SPA Integration:
CookieConsent::handleRequest() middleware to validate consent on API routes:
Route::middleware(['cookie.consent:analytics'])->group(function () {
// Analytics-related API endpoints
});
Session vs. Cookie Storage:
use_cookie in config:
'use_cookie' => true,
Middleware Conflicts:
CookieConsentMiddleware runs on every request. If you experience performance issues, restrict it to specific routes:
Route::middleware(['web', 'cookie.consent'])->group(function () {
// Only apply to web routes
});
Default Consent States:
default to 'required' for a category will block page rendering until consent is given. Test this behavior thoroughly in staging.JavaScript Dependencies:
Localization:
php artisan vendor:publish --provider="Statikbe\CookieConsent\CookieConsentServiceProvider" --tag="lang"
resources/lang/{locale}/cookie-consent.php.Check Consent State:
cookie_consent session variable or cookie to verify user choices:
dd(session('cookie_consent')); // or request()->cookie('cookie_consent')
Disable Modal Temporarily:
'show_modal' => false in config to bypass the UI while developing.Log Consent Events:
CookieConsent facade to log changes:
Statikbe\CookieConsent\Facades\CookieConsent::extend(function ($consent) {
\Log::info('Consent updated', $consent->getAll());
});
Custom Modal Views:
resources/views/vendor/cookie-consent/modal.blade.php.Additional Consent Categories:
public function boot()
{
CookieConsent::addCategory('custom', [
'purpose' => 'Custom tracking',
'default' => 'denied',
]);
}
Consent API:
Route::post('/api/cookie-consent', function (Request $request) {
$consent = $request->validate(['category' => 'required', 'status' => 'required']);
CookieConsent::set($consent['category'], $consent['status']);
return response()->json(['success' => true]);
});
GDPR Compliance:
consent_logs table and middleware to record changes:
CookieConsent::extend(function ($consent) {
\App\Models\ConsentLog::create([
'user_id' => auth()->id(),
'category' => $consent->getCategory(),
'status' => $consent->getStatus(),
'ip' => request()->ip(),
]);
});
How can I help you explore Laravel packages today?