alessandrolandim/cookie-consent-bundle
Installation
composer require connectholland/cookie-consent-bundle
(Note: The package name in the description is alessandrolandim/cookie-consent-bundle, but the README and composer.json use connectholland/cookie-consent-bundle. Use the latter.)
Enable the Bundle
Add to config/bundles.php (Symfony 4/5):
return [
// ...
ConnectHolland\CookieConsentBundle\CHCookieConsentBundle::class => ['all' => true],
];
Enable Routing
Add to config/routes.yaml:
ch_cookie_consent:
resource: "@CHCookieConsentBundle/Resources/config/routing.yaml"
Basic Configuration
Add to config/packages/ch_cookie_consent.yaml:
ch_cookie_consent:
theme: 'light'
categories:
- 'analytics'
- 'tracking'
First Use Case
Render the consent banner in your base template (e.g., base.html.twig):
{{ render_esi(path('ch_cookie_consent.show_if_cookie_consent_not_set')) }}
Conditional Loading
Use show_if_cookie_consent_not_set to avoid rendering the banner after consent is given:
{% if not chcookieconsent_isCookieConsentSavedByUser() %}
{{ render_esi(path('ch_cookie_consent.show_if_cookie_consent_not_set')) }}
{% endif %}
Category-Based Logic Check consent for specific categories (e.g., analytics) before loading scripts:
{% if chcookieconsent_isCategoryAllowedByUser('analytics') %}
{{ include('scripts_analytics.html.twig') }}
{% endif %}
AJAX Submission Include the JS file to enable smooth submission:
{{ asset('bundles/chcookieconsent/js/cookie_consent.js') }}
(Note: Path may vary; verify via bin/console assets:install.)
Locale Support Pass locale dynamically:
{{ render_esi(path('ch_cookie_consent.show_if_cookie_consent_not_set', {
'locale': app.request.locale
})) }}
analytics is allowed.tracking consent.simplified: true in config for a single "Accept All"/"Reject All" option.Routing Conflicts
Ensure ch_cookie_consent routes don’t clash with existing routes. Use _prefix in routing.yaml if needed:
ch_cookie_consent:
resource: "@CHCookieConsentBundle/Resources/config/routing.yaml"
prefix: "/cookie-consent"
Caching Issues
Use render_esi (not render) to bypass Twig cache:
{{ render_esi(path('...')) }} {# Correct #}
{{ render(path('...')) }} {# Avoid #}
CSRF Protection
If csrf_protection: true, ensure your form includes the CSRF token. The bundle handles this automatically for the consent form.
Database Logging
With use_logger: true, verify the CookieConsent entity exists in your DB schema. Run migrations if needed:
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
Application > Cookies) to verify Cookie_Consent_Key and category cookies (e.g., Cookie_Category_analytics).cookie-consent-form-submit-successful.php bin/console cache:clear
Custom Categories
Add new categories to config/packages/ch_cookie_consent.yaml and create translations in translations/messages.{locale}.yml:
ch_cookie_consent:
categories:
- 'custom_category'
# translations/messages.en.yml
CookieConsent:
custom_category: 'Custom Category'
Event Listeners
Subscribe to the cookie-consent-form-submit-successful event in JavaScript:
document.addEventListener('cookie-consent-form-submit-successful', (e) => {
console.log('Consent saved:', e.detail);
// Trigger analytics or other post-consent logic
});
Styling Overrides
Extend the SCSS file (Resources/assets/css/cookie_consent.scss) and rebuild assets:
npm run dev # If using Webpack Encore
php bin/console assets:install
Custom Templates
Override the default template by copying cookie_consent.html.twig to:
templates/bundles/CHCookieConsentBundle/cookie_consent.html.twigapp/Resources/CHCookieConsentBundle/views/cookie_consent.html.twigHow can I help you explore Laravel packages today?