Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Cookie Consent Bundle Laravel Package

connectholland/cookie-consent-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Install via Composer:

composer require harborn-digital/cookie-consent-bundle

Publish the bundle's assets and configuration:

php artisan vendor:publish --provider="HarbornDigital\CookieConsentBundle\CookieConsentServiceProvider" --tag="config"
php artisan vendor:publish --provider="HarbornDigital\CookieConsentBundle\CookieConsentServiceProvider" --tag="public"

Add the bundle to config/app.php under providers if not auto-discovered. First use case: Display the cookie consent banner by adding the Twig template to your layout:

{{ include('CookieConsentBundle::cookieConsent.html.twig') }}

Implementation Patterns

Core Workflow

  1. Configuration: Customize via config/cookie_consent.php (e.g., position, cookie_name, translations).
  2. Usage: The bundle auto-renders the consent banner. Handle consent logic via middleware or controllers:
    use HarbornDigital\CookieConsentBundle\CookieConsentManager;
    
    public function __construct(CookieConsentManager $cookieConsent)
    {
        $this->cookieConsent = $cookieConsent;
    }
    
    public function index()
    {
        if (!$this->cookieConsent->isConsentGiven()) {
            return redirect()->route('home')->with('consent_required', true);
        }
    }
    
  3. Translations: Leverage new Spanish (es) and Catalan (ca) locales by setting locale in config or via middleware:
    $request->setLocale('es'); // Auto-switches translations
    

Integration Tips

  • JavaScript Events: Use the data-consent-accepted event listener for post-consent actions:
    document.addEventListener('consentAccepted', (e) => {
        // Load analytics, ads, etc.
    });
    
  • Middleware: Protect routes requiring consent:
    public function handle($request, Closure $next)
    {
        if (!$this->cookieConsent->isConsentGiven()) {
            abort(403, 'Consent required');
        }
        return $next($request);
    }
    

Gotchas and Tips

Pitfalls

  • RequestStack Fix: Older versions used Request directly. If you extended the bundle, update to use RequestStack (PR #125) to avoid Request not being available in some contexts (e.g., commands).
  • HTML5 Validation: Ensure your custom templates comply with HTML5 validation (fixed in PR #121). Test with:
    php artisan vendor:publish --tag="public" --force
    
    Then validate the published public/cookie-consent/ files.

Debugging

  • Cookie Not Setting: Verify cookie_name in config isn’t conflicting with existing cookies. Use:
    $this->cookieConsent->getCookieName(); // Debug current name
    
  • Translation Issues: Clear cached views and config:
    php artisan view:clear
    php artisan config:clear
    

Extension Points

  • Custom Templates: Override the default Twig template by publishing assets and modifying: resources/views/vendor/cookie-consent/cookieConsent.html.twig.
  • Dynamic Positioning: Use JavaScript to reposition the banner dynamically:
    document.querySelector('.cookie-consent').style.position = 'fixed';
    
  • Locale Switching: Dynamically set the locale via middleware or route middleware:
    public function share(Request $request)
    {
        app()->setLocale($request->get('lang', config('app.locale')));
    }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
thomas-0816/doctrine-dbal-duckdb
moyisoft/laravel-admin
oxygen/mod-pages
oxygen/mod-import-export
oxygen/mod-media
ksoft/laravel-contentbuilderjs
ourgoldpackages/exchange
mikkimike/laravel-exchange1c
wandesnet/mercadopago-laravel
tsfcorp/email
larafony/core
laravel-enso/currencies
tartan/laravel-online-payment
gam/laravel-sat-catalogs
cloudcreativity/laravel-stripe
abdulelahragih/querybuilder
wedevbr/celcoin-laravel
surazdott/nepali-date
devsquad-cockpit/laravel
coding-socks/laravel-upload-handler