Laravel的 GDPR cookie 和 Wirecookies 的同意

2026年8月2日1 次浏览来源:Dev.to阅读原文

正文保留英文原文(机翻易破坏代码与排版),标题/摘要已提供中文

Ship a compliant cookie banner in Laravel and actually gate analytics and marketing scripts on the user's choice, using the wirecookies-saved event and a plain localStorage object as the consent gate.

Wirecookies is a Laravel package which handles the cookies consent for you.

It gives you a consent banner and a preferences modal from a single Blade tag, and, more usefully, it hands you a plain object and a browser event you can use as the gate for your analytics and marketing scripts.

This article is built around that gate, not around how the banner looks.

One thing to get out of the way first, because it will bite you otherwise: Wirecookies ships no JavaScript of its own and uses wiremodal's JS to open the preferences modal.

If you skip the wiremodal import in the install steps, the banner still shows and Accept all / Reject all still work, but the Configure button and the floating re-open button silently do nothing, with no error in the console.

Do the JS step.

How to install Pull the package in with Composer.

The service provider is auto-discovered, so there is nothing to register.

Wirecookies depends on , which Composer pulls in for you.

Now import the stylesheet in , after a base (wiremodal or wiretoast) that defines the theme tokens.

Then bundle wiremodal's JS.

This is the step that makes the Configure and re-open buttons work, so do not skip it.

How to use it Drop the single Blade component once, near the end of your layout.

First-time visitors get a bottom banner after a short delay.

When they choose Accept all, Reject all, or save from the Configure modal, the banner is replaced by a floating button that reopens the preferences panel so they can change their mind later.

That is the whole UI.

The interesting part is what it writes down.

Gating scripts on consent This is the reason to reach for a real consent tool instead of a static banner.

Wirecookies stores the choice as a plain object in under the key , shaped like this: So on the page load you read that object and only boot analytics if the user actually said yes.

That covers returning visitors who already decided.

For the first visit, or when someone changes their choice in the modal, there is nothing in storage yet at load time, so you also listen for the moment they save.

Wirecookies dispatches a event that bubbles up to , and its is the preferences object.

Between those two, the load-time read and the save-time event, you have a complete gate.

Scripts stay dormant until the matching category is true, and they light up the instant the user grants it, without a page reload.

There is no package API to learn for any of this: it is a key and a DOM event, both of which you already know how to use.

Configuring the categories The categories are defined in config, so publish it if you want to change them.

That drops , where each category carries behavior only. is and can never be turned off; the rest are opt-in with a of , which is what keeps everything switched off until the user acts.

The keys here (, , and so on) are the same keys you read off the preferences object in the gate above, which is what ties the config to your JavaScript.

The visible label and description for each category come from the translation files, so you do not hard-code copy in config.

Pointing at your cookie policy The other config key you will touch is the link to your policy. accepts a single string used for every language.

If your policy lives at a different path per language, pass an array keyed by locale and Wirecookies picks the right one from .

Set it to and the link is hidden entirely.

Every config key also has a matching component prop (, , , ) if you would rather override it per render than edit the config file.

The strings themselves ship in Spanish and English out of the box and follow the app locale, with a fallback to .

Wrapping up The banner is the part everyone sees, but the part that keeps you compliant is refusing to load tracking until consent exists.

Wirecookies makes that a two-line job: read from on load, listen for on , and gate each script on the category it needs.

No account, no external script, no tracking of its own, just a plain object in the browser and an event you can hook. 📌 You can see Wirecookies in action at Crowd Legal. 👉 Package on Packagist: https://packagist.org/packages/edulazaro/wirecookies 👉 Source on GitHub: https://github.com/edulazaro/wirecookies

分享