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

jeffersongoncalves/laravel-cookie-consent

Simple Laravel cookie consent banner with GDPR/CCPA-friendly preferences. Drop-in head/body includes, optional view publishing for customization, and settings stored in the database via spatie/laravel-settings with helper access for runtime updates.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require jeffersongoncalves/laravel-cookie-consent
    php artisan migrate
    
  2. Include in Layout: Add these lines to your master template (e.g., resources/views/layouts/app.blade.php):

    @include('cookie-consent::cookie-consent-head')
    @include('cookie-consent::cookie-consent-body')
    
  3. First Use Case:

    • Visit your site to see the default cookie consent banner.
    • Customize via CookieConsentSettings (see below).

Key Files to Review

  • Published Views: resources/views/vendor/cookie-consent/ (after running php artisan vendor:publish --tag=cookie-consent-views)
  • Settings Migration: database/migrations/[timestamp]_create_cookie_consent_settings_table.php
  • Service Provider: vendor/jeffersongoncalves/cookie-consent/src/CookieConsentServiceProvider.php (for extension points)

Implementation Patterns

Core Workflow

  1. Runtime Configuration:

    // In a controller or service
    $settings = cookie_consent_settings();
    $settings->position = 'top-right';
    $settings->popup_background = '#2c3e50';
    $settings->save();
    
    • Use the CookieConsentSettings class or cookie_consent_settings() helper for type-safe access.
  2. Dynamic Theming:

    • Override CSS/JS by updating css_url and js_url in settings:
      $settings->css_url = asset('css/cookie-consent-custom.css');
      $settings->js_url = asset('js/cookie-consent-custom.js');
      
  3. Conditional Rendering:

    • Check consent status in Blade:
      @if(!cookie_consent()->hasConsented())
          <!-- Show banner or custom fallback -->
      @endif
      
    • Use cookie_consent() helper for consent logic.
  4. Multi-Language Support:

    • Store translations in the content_* settings (e.g., content_header, content_message).
    • Update dynamically via admin panel (e.g., Filament/Nova).

Integration Tips

  • Admin Panels:

    • Expose CookieConsentSettings via Filament/Nova for non-developers to manage:
      // Filament example
      use JeffersonGoncalves\CookieConsent\Settings\CookieConsentSettings;
      
      public static function getSettings(): array
      {
          return [
              CookieConsentSettings::class,
          ];
      }
      
  • API Consent:

    • Extend the HasConsent trait to add API-specific consent checks:
      use JeffersonGoncalves\CookieConsent\Traits\HasConsent;
      
      class ApiUser extends Model
      {
          use HasConsent;
      }
      
  • Third-Party Services:

    • Hook into consent events to sync with tools like Google Tag Manager:
      // In EventServiceProvider
      CookieConsent::consented(function ($user) {
          event(new ConsentGranted($user));
      });
      
  • Testing:

    • Mock consent in tests:
      $this->actingAs($user)
           ->withCookie('cookie_consent', 'true')
           ->get('/');
      

Gotchas and Tips

Pitfalls

  1. Migration Pitfalls:

    • v2.0+: Config file (config/cookie-consent.php) is deprecated. All settings now live in the database.
      • Run php artisan migrate after updating to v2.0+.
      • Custom config values must be migrated via code (see Upgrade Guide).
  2. Caching Issues:

    • Clear view cache if customizing published views:
      php artisan view:clear
      
  3. CDN Dependencies:

    • The package defaults to CDN-hosted cookieconsent.min.css/js. For offline use:
      $settings->css_url = asset('vendor/cookieconsent/cookieconsent.min.css');
      $settings->js_url = asset('vendor/cookieconsent/cookieconsent.min.js');
      
    • Publish assets manually if needed:
      php artisan vendor:publish --tag=cookie-consent-assets
      
  4. Consent Persistence:

    • Consent is stored in a cookie (cookie_consent). Ensure your SameSite cookie policy aligns with GDPR requirements:
      // In AppServiceProvider
      Cookie::queue('cookie_consent', 'true', 365 * 24 * 60);
      

Debugging Tips

  1. Inspect Settings:

    • Dump settings to debug:
      dd(cookie_consent_settings()->toArray());
      
  2. Cookie Debugging:

    • Check for consent cookie in browser dev tools (Application > Cookies).
    • Force consent for testing:
      $response = new Response('...');
      $response->withCookie(cookie('cookie_consent', 'true', 365));
      
  3. JavaScript Errors:

    • Verify js_url points to a valid path. Test with:
      $settings->js_url = asset('js/cookieconsent.min.js');
      

Extension Points

  1. Custom Consent Logic:

    • Override the hasConsented() method in a service:
      class CustomConsentService extends CookieConsentService
      {
          public function hasConsented(): bool
          {
              return parent::hasConsented() || auth()->check();
          }
      }
      
    • Bind the service in AppServiceProvider:
      $this->app->bind(CookieConsentService::class, CustomConsentService::class);
      
  2. Event Hooks:

    • Listen for consent events:
      CookieConsent::consented(function ($user) {
          // Sync with CRM, analytics, etc.
      });
      
  3. Custom Views:

    • Publish and override views:
      php artisan vendor:publish --tag=cookie-consent-views
      
    • Modify resources/views/vendor/cookie-consent/cookie-consent.blade.php.
  4. Localization:

    • Use the content_* settings for multi-language support:
      $settings->content_header = __('cookie_consent.header');
      

Pro Tips

  • A/B Testing:

    • Randomize settings for testing:
      $settings->popup_background = request()->ip() % 2 === 0 ? '#2c3e50' : '#e74c3c';
      
  • Dark Mode:

    • Dynamically adjust colors:
      $settings->popup_background = request()->user()->prefers_dark_mode ? '#1a1a1a' : '#696969';
      
  • Analytics Integration:

    • Track consent choices:
      CookieConsent::consented(function ($user) {
          Analytics::track('cookie_consent_granted', ['user_id' => $user->id]);
      });
      
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.
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor