asilingas/cookie-consent-bundle
## Getting Started
### Minimal Setup for Laravel Integration (via Symfony Bridge)
Since this is a Symfony bundle, Laravel developers will need to use **Symfony’s components** or **Laravel’s Symfony bridge** (e.g., `spatie/symfony-bridge`). Below are the minimal steps to integrate it into a Laravel project:
1. **Install via Composer**
```bash
composer require asilingas/cookie-consent-bundle
Bridge Symfony Components If not already set up, install Laravel’s Symfony bridge:
composer require spatie/symfony-bridge
Register the Bundle (via Symfony Kernel)
Create a Symfony kernel class (e.g., app/Kernel.php) and register the bundle:
use ConnectHolland\CookieConsentBundle\CHCookieConsentBundle;
class Kernel extends \Symfony\Component\HttpKernel\Kernel
{
public function registerBundles()
{
return [
new CHCookieConsentBundle(),
// Other Symfony bundles...
];
}
}
Configure Routing
Add the bundle’s routes to Laravel’s Symfony router (e.g., in routes/symfony.php):
use Symfony\Component\Routing\Loader\YamlFileLoader;
use Symfony\Component\Routing\RouteCollection;
$loader = new YamlFileLoader();
$routes = $loader->load(__DIR__.'/../vendor/connectholland/cookie-consent-bundle/Resources/config/routing.yaml');
$symfonyRouter->addCollection($routes);
Publish Config & Assets Publish the bundle’s assets and config:
php artisan vendor:publish --provider="ConnectHolland\CookieConsentBundle\CHCookieConsentBundle" --tag="config"
php artisan vendor:publish --provider="ConnectHolland\CookieConsentBundle\CHCookieConsentBundle" --tag="public"
Add to config/app.php
Ensure the Symfony kernel is bootstrapped in Laravel’s service provider.
First Use Case: Display Cookie Consent In a Blade template (or Twig via Laravel’s bridge), render the consent banner:
@symfony\render_esi(route('ch_cookie_consent.show'))
(Note: Laravel’s render_esi may require customization for Symfony routes.)
Initial Load
chcookieconsent_isCookieConsentSavedByUser in Twig/Blade).@if(!chcookieconsent_isCookieConsentSavedByUser())
@symfony\render_esi(route('ch_cookie_consent.show_if_cookie_consent_not_set'))
@endif
Category-Based Tracking
@if(chcookieconsent_isCategoryAllowedByUser('analytics'))
<script src="https://analytics.example.com/script.js"></script>
@endif
Logging User Preferences
use_logger: true in config to store consent in the database (requires Doctrine ORM).// Example: Fetch user consents (via Symfony Doctrine bridge)
$consents = $entityManager->getRepository(CookieConsentLog::class)->findAll();
Dynamic Theming
php artisan vendor:publish --tag="public" --provider="ConnectHolland\CookieConsentBundle\CHCookieConsentBundle"
resources/sass/cookie_consent.scss and recompile assets.AJAX Submission
<script src="{{ asset('vendor/cookie-consent/js/cookie_consent.js') }}"></script>
document.addEventListener('cookie-consent-form-submit-successful', (e) => {
console.log('Consent saved:', e.detail);
// Trigger analytics scripts dynamically
});
Multi-Language Support
# config/app.php
'fallback_locale' => 'en',
resources/lang/vendor/cookie-consent/.Symfony-Laravel Integration Complexity
spatie/symfony-bridge or manually bridge Symfony components.ch_cookie_consent may clash with Laravel routes).Doctrine ORM Dependency
use_logger or mock the CookieConsentLogger service.Asset Pipeline Conflicts
mix.copy() to include the bundle’s assets:
// webpack.mix.js
mix.copy('vendor/cookie-consent/public/js/cookie_consent.js', 'public/js/');
Cookie Domain/Path Issues
domain or path in config doesn’t match Laravel’s session settings.CookieConsent service to merge with Laravel’s cookie settings:
$this->app->extend('ch_cookie_consent.cookie_consent', function ($consent) {
$consent->setCookieDomain(config('session.domain'));
return $consent;
});
CSRF Protection in AJAX
csrf_protection may need adjustment:
ch_cookie_consent:
csrf_protection: false # Disable if handling CSRF manually
Template Overrides in Laravel
app/templates/bundles/...) won’t work in Laravel. Tip: Use Laravel’s view overrides:
php artisan vendor:publish --tag="views" --provider="ConnectHolland\CookieConsentBundle\CHCookieConsentBundle"
Then modify resources/views/vendor/cookie-consent/cookie_consent.html.twig.Check Consent Status
Cookie_Consent, Cookie_Consent_Key) in browser dev tools. Missing cookies indicate rendering issues.Log Submission Events
$eventDispatcher->addListener('cookie_consent.form.submit', function ($event) {
\Log::debug('Consent submitted:', $event->getData());
});
Verify Database Logs
use_logger is enabled, check the cookie_consent_log table for entries. Anonymized IPs should appear as 0.0.0.0.Clear Cached Views
php artisan view:clear
Custom Categories
ch_cookie_consent:
categories:
- 'analytics'
- 'custom_category' # Add new category
resources/lang/vendor/cookie-consent/en/category.custom_category.php:
return ['Custom Category'];
Custom Storage Backend
CookieConsentStorage service:
$this->app->bind('ch_cookie_consent.storage', function () {
return new CustomCookieConsentStorage();
});
Event-Driven Extensions
cookie-consent.form.submit event to trigger custom logic (e.g., sync with a CRM):
$eventDispatcher->addListener('cookie_consent.form.submit', function ($event) {
// Sync with external service
ExternalService::recordConsent($event->getData());
});
Headless Mode (APIs)
$request->headers->set('X-Cookie-Consent', '{"analytics": true}');
CookieConsentChecker to parse headers instead of cookies.
---
```markdown
## Laravel-Specific Notes
- **Blade vs. Twig**: Replace Twig functions (`chcookieconsent_isCategoryAllowedByUser`) with Laravel
How can I help you explore Laravel packages today?