Installation
Run composer require chaplean/cookie-bundle in your Laravel project (note: this is a Symfony bundle, but can be adapted for Laravel via bridge packages like symfony/bundle or manual integration).
Service Provider Registration
Register the bundle in config/app.php under providers:
'providers' => [
// ...
Chaplean\Bundle\CookieBundle\ChapleanCookieBundle::class,
],
Configuration
Publish the config file (if available) or manually define in config/services.php:
'chaplean_cookie' => [
'learn_more' => '/privacy-policy', // Required URL for cookie details
'translations' => [], // Optional translation overrides
],
Route Inclusion
Add the bundle’s routes in routes/web.php:
Route::prefix('/')->group(function () {
require __DIR__.'/vendor/chaplean/cookie-bundle/Resources/config/routing.yml';
});
Frontend Integration
Include the headband script in your layout (e.g., resources/views/layouts/app.blade.php):
<script src="{{ asset('vendor/chaplean/cookie-bundle/js/headband.js') }}"></script>
Trigger Cookie Banner
Call the JavaScript method to display the banner (e.g., in app.js):
document.addEventListener('DOMContentLoaded', () => {
window.ChapleanCookie.init();
});
First Use Case: Display a cookie consent banner on page load, with a "Learn More" link pointing to your privacy policy.
Dynamic Cookie Banner
localStorage or cookies).public function handle($request, Closure $next) {
if (!$request->user()->hasCookieConsent()) {
return redirect()->route('chaplean_cookie_banner');
}
return $next($request);
}
Translation Management
config/services.php:
'translations' => [
'accept' => 'Agree and Proceed',
'decline' => 'Decline Cookies',
],
Asset Integration
// webpack.mix.js
mix.js('resources/js/app.js', 'public/js')
.copy('vendor/chaplean/cookie-bundle/js/headband.js', 'public/js');
Custom Styling
public/css/headband.css or inline styles:
<style>
.chaplean-cookie-banner { background: #f0f0f0; }
</style>
API for Consent Tracking
// In a service
public function logConsent($userId, $consented) {
Consent::create([
'user_id' => $userId,
'consented' => $consented,
'ip_address' => request()->ip(),
]);
}
Container calls with Laravel’s app() or config().asset() helper for paths instead of Symfony’s path().chaplean.cookie.accepted or chaplean.cookie.declined events (if the bundle emits them) to trigger analytics or other actions.Pest or BrowserKit:
$response = $this->get('/');
$response->assertSee('Cookie Consent Banner');
Symfony vs. Laravel Mismatch
Container and Twig. For Laravel:
container.get() with app() or config().symfony/bundle.Outdated Dependencies
symfony/dependency-injection to ^6.0).Missing JavaScript Initialization
ChapleanCookie.init() isn’t called.DOMContentLoaded or jQuery’s $(document).ready()).Assetic Dependency
No Built-in Consent Storage
session() or cookie() to store consent:
// After accepting cookies
$request->session()->put('cookie_consent', true);
Routing Conflicts
/cookie).routes/web.php:
Route::prefix('admin')->group(function () {
require __DIR__.'/vendor/chaplean/cookie-bundle/Resources/config/routing.yml';
});
Check JavaScript Errors
F12) to verify ChapleanCookie is defined and init() runs without errors.headband.js path. Use {{ asset('js/headband.js') }} in Blade.Verify Configuration
learn_more URL is set:
dd(config('chaplean_cookie'));
Inspect Network Requests
Log Consent Events
Log::info('Cookie consent status:', ['status' => $consented]);
Custom Banner Templates
vendor/chaplean/cookie-bundle/Resources/views/cookie.html.twig to resources/views/vendor/chaplean/cookie-bundle/cookie.html.twig and modify.Add Cookie Categories
// Custom JS extension
window.ChapleanCookie.categories = ['analytics', 'marketing'];
Server-Side Consent Validation
public function handle($request, Closure $next) {
if (!$request->session()->has('cookie_consent')) {
// Disable Google Analytics, etc.
app()->singleton(GoogleAnalytics::class, function () {
return new NullAnalytics();
});
}
return $next($request);
}
GDPR Compliance Hooks
chaplean.cookie.accepted event listener to trigger data processing logs:
Event::listen('chaplean.cookie.accepted', function ($user) {
// Log to GDPR compliance system
});
Localization
config(['chaplean_cookie.translations' => [
'learn_more' => trans('cookie.learn_more'),
]]);
How can I help you explore Laravel packages today?