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

Cookielaw Laravel Package

didublab/cookielaw

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require didublab/cookielaw
    

    Ensure your project is Symfony 4/5 (compatible with Laravel via Symfony Bridge if needed).

  2. Enable the Bundle: Add to config/bundles.php (Symfony) or register via Laravel's service provider equivalent:

    Didublab\CookielawBundle\DidublabCookielawBundle::class => ['all' => true],
    
  3. First Use Case: Insert the Twig function in your base template (e.g., base.html.twig):

    {{ cookie_law() }}
    

    This renders the default cookie consent banner.

  4. Verify Cookie Behavior:

    • Accept the banner and check browser cookies for cookie-notice-accepted.
    • Refresh the page to confirm persistence (default: 10 days).

Implementation Patterns

Core Workflows

  1. Dynamic Placement:

    • Use cookie_law() in Twig templates where legal compliance is required (e.g., footer, modal overlay).
    • Example: Override default styling via CSS:
      .didublab-cookielaw { position: fixed; bottom: 0; width: 100%; }
      
  2. Configuration Management:

    • Centralize settings in config/packages/cookie_law.yaml (Symfony) or Laravel’s config/cookielaw.php:
      didublab_cookielaw:
          cookie_days: 30       # Extend cookie expiry
          cookie_readmore_route: 'privacy_policy'  # Link to policy page
      
    • Laravel Adaptation: Publish config via:
      php artisan vendor:publish --provider="Didublab\CookielawBundle\DidublabCookielawBundle" --tag="config"
      
  3. Translation Integration:

    • Extend translations for multilingual sites:
      • Create translations/cookielaw.en.xliff (Symfony) or resources/lang/en/cookielaw.php (Laravel).
      • Example Laravel translation:
        return [
            'cookie_text' => 'We use cookies to ensure you get the best experience.',
        ];
        
  4. Event-Driven Extensions:

    • Hook into cookie acceptance via Symfony events (e.g., kernel.request):
      // Symfony EventSubscriber
      public function onKernelRequest(GetResponseEvent $event) {
          $request = $event->getRequest();
          if ($request->cookies->has('cookie-notice-accepted')) {
              // Trigger analytics or tracking logic
          }
      }
      
    • Laravel Alternative: Use middleware:
      public function handle($request, Closure $next) {
          if ($request->cookie('cookie-notice-accepted')) {
              // Custom logic
          }
          return $next($request);
      }
      
  5. Conditional Rendering:

    • Skip the banner for exempt users (e.g., admins):
      {% if not app.user.isAdmin %}
          {{ cookie_law() }}
      {% endif %}
      

Gotchas and Tips

Pitfalls

  1. Cookie Domain Scope:

    • Default cookie settings may not persist across subdomains. Explicitly set cookie_domain in config:
      didublab_cookielaw:
          cookie_domain: '.yourdomain.com'  # Include leading dot for subdomains
      
  2. Twig Function Scope:

    • Ensure cookie_law() is only called in Twig templates (not PHP). Symfony’s Twig environment must be properly initialized in Laravel via symfony/twig-bridge.
  3. Translation Overrides:

    • If translations don’t update, clear Symfony’s cache:
      php bin/console cache:clear
      
    • Laravel: Use php artisan view:clear or php artisan config:clear.
  4. Route Configuration:

    • cookie_readmore_route must match an existing Symfony route. For Laravel, use a route alias or map to a controller:
      cookie_readmore_route: 'app.privacy_policy'  # Symfony
      # Laravel: Ensure route exists in `routes/web.php`
      
  5. HTTPS Enforcement:

    • Cookies may not set correctly on http:// sites. Force HTTPS in config:
      didublab_cookielaw:
          cookie_secure: true  # Requires HTTPS
      

Debugging

  1. Cookie Inspection:

    • Use browser dev tools (Application > Cookies) to verify cookie creation/values.
    • Check Symfony’s profiler (/_profiler) for bundle logs.
  2. Template Debugging:

    • Enable Twig debug mode in config/packages/twig.yaml:
      twig:
          debug: true
      
  3. Configuration Validation:

    • Validate YAML syntax with a linter (e.g., YAML Lint).

Extension Points

  1. Custom Templates:

    • Override the default Twig template by creating templates/DidublabCookielawBundle/default/cookie_law.html.twig.
  2. JavaScript Integration:

    • Extend functionality with JS (e.g., auto-accept on scroll):
      document.querySelector('.didublab-cookielaw-accept').addEventListener('click', () => {
          // Custom analytics call
      });
      
  3. Database Backing:

    • Replace cookie storage with a database by extending the bundle’s CookieManager service (Symfony) or creating a Laravel service provider wrapper.
  4. Compliance Logging:

    • Log cookie acceptance events to a database for audits:
      // Symfony Event Listener
      public function onCookieAccepted(CookieEvent $event) {
          $logger->info('Cookie accepted by IP: ' . $event->getRequest()->getClientIp());
      }
      
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.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui