christopheraseidl / livewire-cookie-consent
在 Laravel 中显示 Cookie 允许通知和 Cookie 政策(Livewire + Alpine.js + Tailwind CSS)。
Requires
- php: ^8.0
- livewire/livewire: ^3.0
README
此软件包提供了一种简单的方法来显示 Cookie 允许通知以及可以显示在默认模态框或应用程序其他位置的 Cookie 政策。它需要 Livewire 3 和 Tailwind CSS。
所有希望在欧盟使用的网站都必须遵守一项欧盟法律,该法律规定用户必须被告知——并且必须同意——任何跟踪或分析 Cookie。功能 Cookie 是可选的。为了确保,您可能需要咨询 欧洲委员会指南。
当使用此软件包时,请确保政策文本符合您的需求。您可能需要在发布视图后对其进行编辑。
最后,请注意,此软件包默认附带英语和西班牙语翻译。
安装
您可以通过 composer 安装此软件包
composer require christopheraseidl/livewire-cookie-consent
然后运行安装命令
php artisan cookie-consent:install
这将
- 发布必要的资产;
- 设置
tailwind.config.js
,vite.config.js
和app.css
; - 并运行
npm run build
以编译您的项目用于生产。
该软件包将自动注册其服务提供程序。
如果您正在使用像 Mix 这样的替代资产打包器,您将需要对安装和设置过程进行一些手动调整。
如果您发布视图,您应修改您的 tailwind.config.js
文件,以排除位于 /vendor/christopheraseidl/livewire-cookie-consent
文件夹中的默认设置。
配置
如果您愿意,可以发布配置文件
php artisan vendor:publish --provider="christopheraseidl\CookieConsent\CookieConsentServiceProvider" --tag="cookie-consent-config"
默认情况下,此文件的内容如下
return [ 'cookie_name' => 'cas_laravel_cookie_consent', 'cookies_to_delete' => [], 'consent_value' => 'yes', 'refuse_value' => 'no', 'enabled' => true, 'consent_cookie_lifetime' => 60 * 24 * 365, 'refuse_cookie_lifetime' => 60 * 24 * 30, 'available_locales' => ['en', 'es'], ];
用法
您有两种使用询问用户同意的模态框的方法
选项 1. 直接在模板中包含 Livewire 组件
// Somewhere in your blade template @livewire('cookie-consent')
选项 2. 将中间件添加到内核
// app/Http/Kernel.php class Kernel extends HttpKernel { protected $middleware = [ // ... \christopheraseidl\CookieConsent\CookieConsentMiddleware::class, ]; // ... }
当用户允许 Cookie(通过点击“是”)时,将设置 cas_laravel_cookie_consent
Cookie,并且对话框将隐藏,直到 Cookie 过期,默认情况下,生命周期为 60 * 24 * 365
。
您可以使用提供的服务外观来检查是否已给出同意:CookieConsent::consentGiven()
。
您可以使用类似的方法来检查是否拒绝同意:CookieConsent::consentRefused()
。
包含 Cookie 政策组件
此软件包提供的模态框已包含 Cookie 政策组件,您可以在应用程序的任何位置包含它,如下所示
<x-cookie-policy />
您可以通过发布视图并编辑生成的组件视图文件来自定义此政策。
如果您希望提供其他语言的策略,首先 设置应用程序以使用您的区域设置,然后在 /resources/views/christopheraseidl/policy/
中创建一个用于您的区域设置的目录,然后在该目录中放置一个名为 cookie-policy.blade.php
的文件
禁用软件包
要停止此软件包显示模态框或处理 Cookie 允许,在发布配置文件后,将 enabled
更改为 false
。
在拒绝同意时删除 Cookie 数组
如果您想在用户拒绝同意时删除应用程序中的cookies,请发布配置文件,并将它们的名称添加到cookies_to_delete
数组中。例如
//config/cookie-consent.php return [ // ... 'cookies_to_delete' => ['delete_me', 'delete_me_also'], ];
自定义翻译
您可以使用以下命令发布翻译到resources/lang/christopheraseidl/cookie-consent
php artisan vendor:publish --provider="christopheraseidl\CookieConsent\CookieConsentServiceProvider" --tag="cookie-consent-translations"
自定义视图
您可以使用以下命令发布视图到resources/views/christopheraseidl/cookie-consent
php artisan vendor:publish --provider="christopheraseidl\CookieConsent\CookieConsentServiceProvider" --tag="cookie-consent-views"
卸载
在运行composer remove christopheraseidl/livewire-cookie-consent
之前,您应该运行以下命令来清理此包
php artisan cookie-consent:uninstall
测试
vendor/bin/phpunit
许可证
MIT许可证(MIT)。请参阅许可证文件获取更多信息。