Installation
composer require ekyna/cookie-consent-bundle
Ensure compatibility with your Laravel version (this bundle is Symfony-based; use Laravel Bridge if needed).
Register Bundle
In config/app.php, add the bundle to the providers array:
Ekyna\Bundle\CookieConsentBundle\EkynaCookieConsentBundle::class,
Add Routes
Include the bundle’s routes in routes/web.php:
require __DIR__.'/../../vendor/ekyna/cookie-consent-bundle/src/Resources/config/routing.yaml';
First Use Case
Add the Twig functions to your base template (resources/views/layouts/app.blade.php):
@extends('layouts.base')
@section('styles')
<link href="{{ asset('vendor/ekyna/cookie-consent-bundle/css/cookie-consent.css') }}" rel="stylesheet">
@endsection
@section('scripts')
@if(!ekyna_cookie_consent_category_allowed('all'))
{!! ekyna_cookie_consent_render() !!}
@endif
@endsection
Consent Collection
ekyna_cookie_consent_render() in your layout to display the consent modal..cookie-consent class).Conditional Script Loading
Check consent for categories (analytic, marketing, social_network) before loading third-party scripts:
{% if ekyna_cookie_consent_category_allowed('analytic') %}
<script src="https://analytics.example.com/script.js"></script>
{% endif %}
API Integration
For AJAX-heavy apps, use the API endpoint (/cookie-consent/accept) to programmatically handle consent:
fetch('/cookie-consent/accept', {
method: 'POST',
headers: { 'X-Requested-With': 'XMLHttpRequest' },
body: JSON.stringify({ categories: ['analytic', 'marketing'] })
});
Laravel-Specific Adaptations
AppServiceProvider:
$this->app->bind('ekyna.cookie_consent.manager', function ($app) {
return new CustomConsentManager($app['ekyna.cookie_consent.manager']);
});
public function handle($request, Closure $next) {
if (!$request->hasCookie('cookie_consent') || !ekyna_cookie_consent_category_allowed('required_category')) {
abort(403);
}
return $next($request);
}
Dynamic Configuration
Override default settings in config/packages/ekyna_cookie_consent.yaml:
categories:
analytic:
name: "Analytics"
description: "Track user behavior"
default: false
Symfony Dependency
class CookieConsentService {
public function render(array $options = []) {
// Delegate to Symfony's Twig environment if available
}
}
Cookie Scope
cookie_consent. Ensure no conflicts with existing cookies (e.g., laravel_session).config(['cookie_consent.cookie_name' => 'custom_consent']) to override.Twig Environment Mismatch
Blade::directive('ekyna_cookie_consent_render', function ($expr) {
return "<?php echo app('ekyna.cookie_consent.twig')->render($expr); ?>";
});
CSRF Protection
/cookie-consent/accept endpoint requires CSRF tokens. For AJAX, include:
headers: {
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content
}
Check Consent State
Inspect the cookie_consent cookie or dump the manager:
dd(app('ekyna.cookie_consent.manager')->getConsent());
Log Consent Events Extend the manager to log actions:
class CustomManager extends \Ekyna\Bundle\CookieConsentBundle\Manager {
public function accept(array $categories) {
\Log::info('Consent accepted', ['categories' => $categories]);
return parent::accept($categories);
}
}
Override Templates
Customize the modal by copying vendor/ekyna/cookie-consent-bundle/Resources/views/ to resources/views/vendor/ekyna/cookie-consent-bundle/.
Add Custom Categories Dynamically register categories via the manager:
$manager->addCategory('custom', [
'name' => 'Custom Tracking',
'default' => false
]);
Localization
Override translations in config/packages/ekyna_cookie_consent.yaml:
translations:
en:
accept: "Accept All"
reject: "Reject All"
Database Storage Replace the default cookie storage with a database-backed solution:
$manager->setStorage(new DatabaseConsentStorage());
How can I help you explore Laravel packages today?