Installation:
composer require didublab/cookielaw
Ensure your project is Symfony 4/5 (compatible with Laravel via Symfony Bridge if needed).
Enable the Bundle:
Add to config/bundles.php (Symfony) or register via Laravel's service provider equivalent:
Didublab\CookielawBundle\DidublabCookielawBundle::class => ['all' => true],
First Use Case:
Insert the Twig function in your base template (e.g., base.html.twig):
{{ cookie_law() }}
This renders the default cookie consent banner.
Verify Cookie Behavior:
cookie-notice-accepted.Dynamic Placement:
cookie_law() in Twig templates where legal compliance is required (e.g., footer, modal overlay)..didublab-cookielaw { position: fixed; bottom: 0; width: 100%; }
Configuration Management:
config/packages/cookie_law.yaml (Symfony) or Laravel’s config/cookielaw.php:
didublab_cookielaw:
cookie_days: 30 # Extend cookie expiry
cookie_readmore_route: 'privacy_policy' # Link to policy page
php artisan vendor:publish --provider="Didublab\CookielawBundle\DidublabCookielawBundle" --tag="config"
Translation Integration:
translations/cookielaw.en.xliff (Symfony) or resources/lang/en/cookielaw.php (Laravel).return [
'cookie_text' => 'We use cookies to ensure you get the best experience.',
];
Event-Driven Extensions:
kernel.request):
// Symfony EventSubscriber
public function onKernelRequest(GetResponseEvent $event) {
$request = $event->getRequest();
if ($request->cookies->has('cookie-notice-accepted')) {
// Trigger analytics or tracking logic
}
}
public function handle($request, Closure $next) {
if ($request->cookie('cookie-notice-accepted')) {
// Custom logic
}
return $next($request);
}
Conditional Rendering:
{% if not app.user.isAdmin %}
{{ cookie_law() }}
{% endif %}
Cookie Domain Scope:
cookie_domain in config:
didublab_cookielaw:
cookie_domain: '.yourdomain.com' # Include leading dot for subdomains
Twig Function Scope:
cookie_law() is only called in Twig templates (not PHP). Symfony’s Twig environment must be properly initialized in Laravel via symfony/twig-bridge.Translation Overrides:
php bin/console cache:clear
php artisan view:clear or php artisan config:clear.Route Configuration:
cookie_readmore_route must match an existing Symfony route. For Laravel, use a route alias or map to a controller:
cookie_readmore_route: 'app.privacy_policy' # Symfony
# Laravel: Ensure route exists in `routes/web.php`
HTTPS Enforcement:
http:// sites. Force HTTPS in config:
didublab_cookielaw:
cookie_secure: true # Requires HTTPS
Cookie Inspection:
/_profiler) for bundle logs.Template Debugging:
config/packages/twig.yaml:
twig:
debug: true
Configuration Validation:
Custom Templates:
templates/DidublabCookielawBundle/default/cookie_law.html.twig.JavaScript Integration:
document.querySelector('.didublab-cookielaw-accept').addEventListener('click', () => {
// Custom analytics call
});
Database Backing:
CookieManager service (Symfony) or creating a Laravel service provider wrapper.Compliance Logging:
// Symfony Event Listener
public function onCookieAccepted(CookieEvent $event) {
$logger->info('Cookie accepted by IP: ' . $event->getRequest()->getClientIp());
}
How can I help you explore Laravel packages today?