Weave Code
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

Laravel Cookie Consent Laravel Package

devrabiul/laravel-cookie-consent

GDPR-compliant cookie consent for Laravel with one-click setup and no frontend dependencies. Fully customizable banners with RTL/i18n, dark mode, responsive UI, and granular category controls (necessary/analytics/marketing) for enterprise-grade compliance.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:
    composer require devrabiul/laravel-cookie-consent
    php artisan vendor:publish --provider="Devrabiul\CookieConsent\CookieConsentServiceProvider"
    
  2. Basic Integration: Add to your Blade template's <head> and before </body>:
    {!! CookieConsent::styles() !!}
    <!-- Your content -->
    {!! CookieConsent::scripts() !!}
    

First Use Case

Deploy a GDPR-compliant cookie banner with default settings. Test by:

  • Refreshing the page to see the banner.
  • Accepting/rejecting cookies to verify cookie storage.
  • Checking if the banner disappears after consent.

Implementation Patterns

Core Workflow

  1. Configuration: Customize config/cookie-consent.php for:

    • Cookie categories (e.g., necessary, analytics).
    • Lifetimes, themes, and translations.
    • Policy links (e.g., Privacy Policy URL).
  2. Dynamic Loading: Use CookieConsent::scripts(options: [...]) to pass runtime overrides (e.g., disable page interaction):

    {!! CookieConsent::scripts([
        'disable_page_interaction' => false,
        'theme' => 'trust-green'
    ]) !!}
    
  3. Category-Based Consent: Define JavaScript actions per category (e.g., js_action: 'loadGoogleAnalytics' for analytics cookies). Implement these in your global JS:

    function loadGoogleAnalytics() { /* GA script */ }
    
  4. Multi-Language Support: Override translations in resources/lang/{locale}/cookie-consent.php:

    return [
        'Privacy Policy' => 'Datenschutzrichtlinie',
    ];
    
  5. Dark Mode/RTL: Add theme="dark" or dir="rtl" to <body> for automatic adaptation.

Integration Tips

  • Layout Flexibility: Choose from 6 banner styles (box, bar, cloud, etc.) via consent_modal_layout.
  • Preferences Modal: Enable with 'preferences_modal_enabled' => true for granular user control.
  • Compliance Links: Add a "Change Preferences" link anywhere:
    <a onclick="showHideToggleCookiePreferencesModal()">Update Preferences</a>
    
  • Asset Customization: Override default assets by publishing views:
    php artisan vendor:publish --tag=cookie-consent-views
    

Gotchas and Tips

Pitfalls

  1. Cookie Prefix Conflicts: Ensure cookie_prefix (e.g., Laravel_App) doesn’t clash with existing cookies. Default is Laravel_CookieConsent.

  2. JavaScript Actions: Forgetting to implement js_action functions (e.g., loadGoogleAnalytics) will break dependent scripts. Test in console:

    loadGoogleAnalytics(); // Should load GA script
    
  3. Cached Assets: Clear view/compiled assets after theme/config changes:

    php artisan view:clear
    php artisan cache:clear
    
  4. Localhost Asset Paths: Set COOKIE_CONSENT_ASSET_URL=null in .env for localhost to avoid CORS issues.

  5. Dark Mode Detection: Auto-detection may fail if <body> lacks theme="dark". Fallback to manual override:

    {!! CookieConsent::scripts(['theme' => 'dark']) !!}
    

Debugging

  • Check Consent Status: Inspect cookies (Laravel_CookieConsent_necessary, Laravel_CookieConsent_analytics) or use:

    $consent = CookieConsent::getConsent();
    dd($consent); // Returns array of user choices
    
  • Event Listeners: Listen for consent changes via events:

    // In EventServiceProvider
    protected $listen = [
        'Devrabiul\CookieConsent\Events\ConsentChanged' => [
            'App\Listeners\LogConsentChange',
        ],
    ];
    

Extension Points

  1. Custom Themes: Extend the theme_preset system by publishing assets:

    php artisan vendor:publish --tag=cookie-consent-public
    

    Then override resources/css/cookie-consent.css.

  2. Additional Categories: Add new categories in cookie_categories config and implement corresponding JS actions.

  3. API Integration: Fetch consent status via API:

    route('cookie-consent.status'); // Add to routes/web.php
    

    Return JSON response with current consent data.

  4. Third-Party Scripts: Use the js_action system to load scripts conditionally:

    function loadHotjar() { /* Hotjar script */ }
    

    Then map it in config:

    'hotjar' => ['js_action' => 'loadHotjar', ...],
    

Pro Tips

  • A/B Testing: Use consent_modal_layout to test different banner styles without code changes.
  • Environment-Specific Config: Leverage .env for toggling categories:
    COOKIE_CONSENT_ANALYTICS=false # Disable in staging
    
  • Performance: Lazy-load scripts by setting disable_page_interaction: true until consent is given.
  • Accessibility: Ensure contrast ratios meet WCAG standards by customizing theme_preset colors.
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.
codraw/framework-extra-bundle
codraw/messenger
codraw/security
codraw/mailer
codraw/contracts
codraw/profiling
codraw/dependency-injection
codraw/tester
codraw/core
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony