Installation
Run composer require elao/consent-bundle and enable the bundle in config/bundles.php:
Elao\Bundle\ConsentBundle\ElaoConsentBundle::class => ['all' => true],
Verify the bundle loads by checking for the consent toast in your frontend.
Default Consent Check Use the built-in Twig helper to conditionally load scripts:
{% if has_user_consent() %}
{{ include('scripts/analytics.js') }}
{% endif %}
This immediately enforces GDPR compliance for default tracking scripts.
Quick Configuration
Override the cookie name/TTL in config/packages/elao_consent.yaml:
elao_consent:
cookie:
name: "user_consent"
ttl: 31536000 # 1 year
Multi-Consent Management
Define granular consents in config/packages/elao_consent.yaml:
elao_consent:
consents:
analytics: { label: "Analytics Tracking" }
marketing: { label: "Marketing Cookies" }
Check consents individually in Twig:
{% if has_user_consent('analytics') %}
{{ include('scripts/ga4.js') }}
{% endif %}
Dynamic Script Loading
Use Twig’s else to provide fallback content:
{% if has_user_consent('marketing') %}
{{ include('scripts/retargeting.js') }}
{% else %}
{{ include('scripts/placeholder.html') }}
{% endif %}
Event-Based Integration
Listen for consent events (e.g., elao_consent.granted) to trigger actions:
// src/EventListener/ConsentListener.php
public function onConsentGranted(ConsentEvent $event) {
if ($event->getConsentName() === 'analytics') {
$this->analyticsService->initialize();
}
}
Register in services.yaml:
services:
App\EventListener\ConsentListener:
tags: ['kernel.event_listener', { event: 'elao_consent.granted' }]
API-Centric Consent Checks Create a controller to expose consent status via API:
// src/Controller/ConsentController.php
public function checkConsent(Request $request, ConsentManager $manager) {
return response()->json([
'analytics' => $manager->hasConsent('analytics'),
'marketing' => $manager->hasConsent('marketing')
]);
}
Custom Toast Templates
Override the default toast in templates/bundles/elaoconsent/toast.html.twig to match your theme.
Consent-Dependent Routes Use middleware to block routes requiring consent:
// src/Http/Middleware/RequireConsent.php
public function handle(Request $request, Closure $next) {
if (!$this->consentManager->hasConsent('analytics') && $request->is('analytics/*')) {
abort(403);
}
return $next($request);
}
Cookie Domain Mismatch
If the toast doesn’t persist, ensure the cookie domain in elao_consent.cookie.domain matches your app’s domain (e.g., .example.com for subdomains).
Caching Issues
Clear Symfony’s cache (php bin/console cache:clear) after changing consent configurations.
Twig Helper Scope
has_user_consent() only works in Twig templates. For PHP logic, use the service:
$this->consentManager->hasConsent('foobar');
Default Consent Override
If you replace the default consent, ensure all legacy checks use the new name (e.g., has_user_consent('foobar') instead of has_user_consent()).
Check Cookie Values
Inspect the consent cookie (or your custom name) in browser dev tools to verify consent storage.
Event Debugging
Enable debug mode (APP_DEBUG=true) to see fired events in the Symfony profiler.
CSS Variable Conflicts
If colors don’t apply, ensure your CSS uses the correct variable names (e.g., --elao-consent-primary).
Custom Consent Logic
Extend the ConsentManager service to add business rules:
// src/Service/CustomConsentManager.php
public function hasConsent(string $name): bool {
if ($name === 'premium_features') {
return $this->user->isPremium();
}
return parent::hasConsent($name);
}
Database-Backed Consents
Override the cookie storage with a database layer by implementing Elao\Bundle\ConsentBundle\Storage\ConsentStorageInterface.
Localization
Translate consent labels by extending the bundle’s translation files in translations/messages.en.yaml:
consent:
foobar: "Accept %name% tracking"
How can I help you explore Laravel packages today?