Weave 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

Cookie Notice Laravel Package

duncanmcclean/cookie-notice

Cookie Notice is a lightweight Statamic addon that adds a configurable cookie consent banner to your site. Customize the message and policy link, style it to match your theme, and remember visitors’ consent with a simple cookie. Easy to set up and suitable for most sites.

View on GitHub
Deep Wiki
Context7

Getting Started

  • Install via Composer: composer require duncanmcclean/cookie-notice
  • Publish the config file: php artisan vendor:publish --tag=cookie-notice-config
  • Register the middleware DuncanMcClean\CookieNotice\Http\Middleware\InjectCookieNotice in your web middleware group (in app/Http/Kernel.php for Laravel ≤10, or bootstrap/app.php for Laravel 11+). This automatically injects the notice before </body>.
  • Configure basic options in config/cookie-notice.php: enable the notice, set the text, buttons, cookie name, and expiry.
  • First use case: conditionally load analytics scripts. In a Blade view:
    @if(CookieNotice::hasAccepted('analytics'))
        <script src="https://analytics.example.com/script.js"></script>
    @endif
    

Implementation Patterns

  • Gate scripts by category: Define categories (e.g., analytics, marketing) in the config, then use CookieNotice::hasAccepted('analytics') in Blade or CookieNotice::hasBeenAccepted() for general consent.
  • Customize appearance: Publish views (php artisan vendor:publish --tag=cookie-notice-views) and edit the notice Blade file. Publish assets if you need to modify CSS/JS.
  • Multi-language: Publish translations (php artisan vendor:publish --tag=cookie-notice-translations) and add locale files.
  • Server-side consent checks: Use the CookieNotice facade in controllers or middleware to conditionally enable server-side tracking or features.
  • Testing: Set the consent cookie in tests, e.g. $this->withCookie('cookie_notice', json_encode(['accepted' => ['analytics']])), or use the package’s testing helpers if available.

Gotchas and Tips

  • Middleware order: Ensure the middleware runs within the web group. If using full-page caching, the notice may be cached—exclude pages or render the notice dynamically.
  • Cookie name and expiry: Changing cookie_name after users have consented resets their preferences. Keep it stable.
  • v10.2.2 fix: Upgrading to this release eliminates the brief flash of the widget when preferences are already set. If you noticed a flicker on page load, this is the fix.
  • Custom views: The package’s JavaScript relies on specific classes/IDs. Test thoroughly after customizing the Blade template.
  • Config cache: Always run php artisan config:clear after editing config/cookie-notice.php.
  • SPAs / dynamic consent: If the package provides a JS API, use it to update consent without a full page reload; otherwise, listen for the built-in consent-change event if available.
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.
novasa/laravel-language-center
opencage/geocode
adolfoholzer/parameters
livenetworks/ln-starter
shitoudev/permissionmanager
johannesclimacus/laravel-tui
hocvt/seotools
vitalybaev/sypexgeo
amphibee/themosis-ignition
gito-me/fast-excel
frankyso/laravel-ipaymu
geolize-studio/laravel-chile
boomerangui/chat
cgv/localized-resource
noith/settings
kda/laravel-package
kda/filament-dynamic-navigation
bypersoft/signbox
pairus/product-data
fayne/laravel-filament-cms-core