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

spatie/laravel-cookie-consent

Add a simple, customizable cookie consent banner to Laravel. Shows on first visit, stores consent, then stays hidden. No “decline” option, no tracker blocking, and no consent categories—use other tools if you need advanced compliance features.

View on GitHub
Deep Wiki
Context7

Getting Started

  1. Install via Composer: composer require spatie/laravel-cookie-consent
  2. Optionally publish the config: php artisan vendor:publish --provider="Spatie\CookieConsent\CookieConsentServiceProvider" --tag="cookie-consent-config"
  3. Add the consent banner to your Blade layout: @include('cookie-consent::index')
  4. For global auto-injection (recommended), register the middleware in your app kernel (see Usage section in README)

First use case: quickly add EU-compliant cookie consent to an existing Laravel app by embedding the view in your main layout—no configuration needed.

Implementation Patterns

  • Minimal integration: Drop @include('cookie-consent::index') into your main Blade layout (e.g., layouts/app.blade.php). The banner appears only once per user (stored in cookie).
  • Middleware approach: Add \Spatie\CookieConsent\CookieConsentMiddleware::class to global middleware stack to auto-append banner without manual Blade includes—ideal for template-heavy apps.
  • Localization: Publish translation files (--tag="cookie-consent-translations"), then add locale files like resources/lang/vendor/cookie-consent/fr/texts.php.
  • Full customization: Publish views (--tag="cookie-consent-views"), then override dialogContents.blade.php to modify structure, and index.blade.php to tweak JS behavior.
  • Tailwind styling: Default output uses Tailwind CSS v2 classes; extend .js-cookie-consent via CSS for custom positioning (e.g., fixed bottom-right corner).
  • Disabling conditionally: Set COOKIE_CONSENT_ENABLED=false in .env (e.g., for local dev) to suppress the banner.

Gotchas and Tips

  • Cookie domain mismatch: Ensure SESSION_DOMAIN is set correctly in .env. The banner won’t persist across subdomains without this.
  • No “Decline” option: This package only handles one-click "Accept". For granular consent (e.g., functional vs. marketing), consider alternatives like whitecube/laravel-cookie-consent.
  • Middleware ordering matters: If using middleware, place it after TrustProxies and before PreventRequestsDuringMaintenance to avoid cookie domain issues.
  • Caching gotcha: If using full-page caching (e.g., Laravel Cache Pages), ensure the banner is not cached—consider using Blade @include with SSR fallback or dynamic view rendering.
  • Cookie set only on accept: Users who close the banner without clicking “Allow” won’t have the consent cookie set; if required, extend the CookieConsentMiddleware to handle that case.
  • Testing tip: Clear the laravel_cookie_consent cookie and set APP_ENV=local/COOKIE_CONSENT_ENABLED=true in .env.testing to ensure tests hit the consent flow.
  • Avoid double banners: If using middleware + manual @include, remove the explicit include to prevent duplicate banners.
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport