Installation
composer require c2is/cookie-bundle ~1.0@dev
Ensure FOSJsRoutingBundle is also installed (required for JS routing).
Register Bundles
Add to app/AppKernel.php:
new FOS\JsRoutingBundle\FOSJsRoutingBundle(),
new C2is\Bundle\CookieBundle\C2isCookieBundle(),
Routing Setup
Add to app/config/routing.yml:
c2is_cookie:
resource: "@C2isCookieBundle/Resources/config/routing.yml"
fos_js_routing:
resource: "@FOSJsRoutingBundle/Resources/config/routing/routing.xml"
Display the Cookie Banner Embed the banner in your layout using an ESI include (recommended for caching):
{% esi '@C2isCookieBundle:Default:cookieBanner' %}
Or directly include the Twig template:
{% render url('c2is_cookie_default_cookie_banner') %}
Default Behavior
accepted=true).dismissed=3).config.yml (see Gotchas).Custom Templates Override the default Twig template by creating a new template at:
app/Resources/C2isCookieBundle/views/Default/cookieBanner.html.twig
Extend the base template or replace entirely.
JavaScript Logic
The bundle relies on fos_js_routing for AJAX calls to:
/cookie/accept (accept cookies)./cookie/dismiss (dismiss banner).
Override JS behavior by extending the cookieBundle.js asset (see Extension Points).Conditional Display Hide the banner for specific routes/users by checking cookies in Twig:
{% if not app.session.has('cookie_accepted') and not app.session.has('cookie_dismissed') %}
{% esi '@C2isCookieBundle:Default:cookieBanner' %}
{% endif %}
Symfony Forms Integration If using Symfony Forms, validate cookie acceptance before submission:
use C2is\Bundle\CookieBundle\Validator\Constraints\CookieAcceptance;
$builder->add('submit', SubmitType::class, [
'constraints' => [new CookieAcceptance()],
]);
Routing Conflicts
fos_js_routing is loaded before C2isCookieBundle in AppKernel.routing.yml to avoid clashes.Cookie Persistence
config.yml:
c2is_cookie:
cookie_lifetime: 31536000 # 1 year in seconds
max_dismissals: 3 # Default: 3 dismissals before hiding
Response headers for Set-Cookie.Caching Issues
cache:clear after template changes.{% esi '@C2isCookieBundle:Default:cookieBanner' with {'cache_time': 0} %}
JavaScript Errors
fos_js_routing.js is loaded (check browser console)./_fos_js_routing/js.// app/Resources/public/js/cookieBundle.js
require(['jquery'], function($) {
$(document).ready(function() {
// Custom logic here
});
});
Then update the template to load your version:
{{ parent() }} {# Extends default template #}
{{ js_path('bundles/c2iscookie/js/cookieBundle.js') }}
Twig Template Overrides
Default/cookieBanner.html.twig).{% extends '@C2isCookieBundle/Default/cookieBanner.html.twig' %}
Custom Cookie Names Override cookie names in config:
c2is_cookie:
cookie_name: 'my_custom_cookie'
accepted_cookie: 'accepted_flag'
dismissed_cookie: 'dismissed_count'
Event Listeners Hook into cookie actions via Symfony events:
// src/C2isCookieBundle/EventListener/CookieListener.php
class CookieListener implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
'c2is_cookie.accept' => 'onCookieAccept',
'c2is_cookie.dismiss' => 'onCookieDismiss',
];
}
public function onCookieAccept(CookieEvent $event) { /* ... */ }
}
Dispatch events in the bundle’s controller (requires extending the bundle).
Multi-Language Support Localize the banner by overriding the Twig template and using Symfony’s translation system:
{{ 'cookie.banner.message'|trans }}
Define translations in app/Resources/translations/messages.en.yml.
How can I help you explore Laravel packages today?