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

Consent Bundle Laravel Package

elao/consent-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

First Steps

  1. Installation Run composer require elao/consent-bundle and enable the bundle in config/bundles.php:

    Elao\Bundle\ConsentBundle\ElaoConsentBundle::class => ['all' => true],
    

    Verify the bundle loads by checking for the consent toast in your frontend.

  2. Default Consent Check Use the built-in Twig helper to conditionally load scripts:

    {% if has_user_consent() %}
        {{ include('scripts/analytics.js') }}
    {% endif %}
    

    This immediately enforces GDPR compliance for default tracking scripts.

  3. Quick Configuration Override the cookie name/TTL in config/packages/elao_consent.yaml:

    elao_consent:
        cookie:
            name: "user_consent"
            ttl: 31536000 # 1 year
    

Implementation Patterns

Core Workflows

  1. Multi-Consent Management Define granular consents in config/packages/elao_consent.yaml:

    elao_consent:
        consents:
            analytics: { label: "Analytics Tracking" }
            marketing: { label: "Marketing Cookies" }
    

    Check consents individually in Twig:

    {% if has_user_consent('analytics') %}
        {{ include('scripts/ga4.js') }}
    {% endif %}
    
  2. Dynamic Script Loading Use Twig’s else to provide fallback content:

    {% if has_user_consent('marketing') %}
        {{ include('scripts/retargeting.js') }}
    {% else %}
        {{ include('scripts/placeholder.html') }}
    {% endif %}
    
  3. Event-Based Integration Listen for consent events (e.g., elao_consent.granted) to trigger actions:

    // src/EventListener/ConsentListener.php
    public function onConsentGranted(ConsentEvent $event) {
        if ($event->getConsentName() === 'analytics') {
            $this->analyticsService->initialize();
        }
    }
    

    Register in services.yaml:

    services:
        App\EventListener\ConsentListener:
            tags: ['kernel.event_listener', { event: 'elao_consent.granted' }]
    
  4. API-Centric Consent Checks Create a controller to expose consent status via API:

    // src/Controller/ConsentController.php
    public function checkConsent(Request $request, ConsentManager $manager) {
        return response()->json([
            'analytics' => $manager->hasConsent('analytics'),
            'marketing' => $manager->hasConsent('marketing')
        ]);
    }
    

Advanced Patterns

  • Custom Toast Templates Override the default toast in templates/bundles/elaoconsent/toast.html.twig to match your theme.

  • Consent-Dependent Routes Use middleware to block routes requiring consent:

    // src/Http/Middleware/RequireConsent.php
    public function handle(Request $request, Closure $next) {
        if (!$this->consentManager->hasConsent('analytics') && $request->is('analytics/*')) {
            abort(403);
        }
        return $next($request);
    }
    

Gotchas and Tips

Common Pitfalls

  1. Cookie Domain Mismatch If the toast doesn’t persist, ensure the cookie domain in elao_consent.cookie.domain matches your app’s domain (e.g., .example.com for subdomains).

  2. Caching Issues Clear Symfony’s cache (php bin/console cache:clear) after changing consent configurations.

  3. Twig Helper Scope has_user_consent() only works in Twig templates. For PHP logic, use the service:

    $this->consentManager->hasConsent('foobar');
    
  4. Default Consent Override If you replace the default consent, ensure all legacy checks use the new name (e.g., has_user_consent('foobar') instead of has_user_consent()).

Debugging Tips

  • Check Cookie Values Inspect the consent cookie (or your custom name) in browser dev tools to verify consent storage.

  • Event Debugging Enable debug mode (APP_DEBUG=true) to see fired events in the Symfony profiler.

  • CSS Variable Conflicts If colors don’t apply, ensure your CSS uses the correct variable names (e.g., --elao-consent-primary).

Extension Points

  1. Custom Consent Logic Extend the ConsentManager service to add business rules:

    // src/Service/CustomConsentManager.php
    public function hasConsent(string $name): bool {
        if ($name === 'premium_features') {
            return $this->user->isPremium();
        }
        return parent::hasConsent($name);
    }
    
  2. Database-Backed Consents Override the cookie storage with a database layer by implementing Elao\Bundle\ConsentBundle\Storage\ConsentStorageInterface.

  3. Localization Translate consent labels by extending the bundle’s translation files in translations/messages.en.yaml:

    consent:
        foobar: "Accept %name% tracking"
    
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.
comsave/common
alecsammon/php-raml-parser
chrome-php/wrench
lendable/composer-license-checker
typhoon/reflection
mesilov/moneyphp-percentage
mike42/gfx-php
bookdown/themes
aura/view
aura/html
aura/cli
povils/phpmnd
nayjest/manipulator
omnipay/tests
psr-mock/http-message-implementation
psr-mock/http-factory-implementation
psr-mock/http-client-implementation
voku/email-check
voku/urlify
rtheunissen/guzzle-log-middleware