justijndepover / laravel-cookie-consent
使您的Laravel应用程序符合欧盟的Cookie法规。
0.6.0
2023-10-14 09:02 UTC
Requires
- laravel/framework: ^8.0 || ^9.0 || ^10.0
README
使您的Laravel应用程序符合欧盟的Cookie法规。
注意
此应用程序仍在开发中,可能会实现破坏性更改。请自行承担风险使用。
说明
此软件包将所有Cookie存储在数据库中。您的Laravel应用程序的用户可以启用/禁用每个Cookie。接受Cookie横幅将在同一请求中加载所有Cookie并执行它们。(这对于页面跟踪器来说非常好)
将在您的应用程序中添加一个带有2个选项的Cookie横幅
- 接受:将加载所有Cookie脚本。(通过同一请求和所有并发请求中的javascript)
- 拒绝:不会加载任何Cookie脚本。
在确认/拒绝Cookie横幅后,用户仍然可以更改其偏好设置。
Cookie值将始终包含一个加密的列表,其中包含已关闭的Cookie ID。因此,添加新的Cookie时,必须由最终用户再次禁用。
安装
您可以使用composer安装此软件包
composer require justijndepover/laravel-cookie-consent
安装后,如果您没有cookie表/模型,必须发布迁移
php artisan vendor:publish --tag="laravel-cookie-consent-migration"
php artisan migrate
可选地发布配置文件
php artisan vendor:publish --tag="laravel-cookie-consent-config"
模型设置
您的Cookie
类也应使用InteractsWithCookies
特质。
use Justijndepover\CookieConsent\Concerns\InteractsWithCookies; // add this line class Cookie { use InteractsWithCookies; // add this line }
配置
这是配置文件
return [ /* * Use this setting to enable the cookie consent dialog. */ 'enabled' => env('COOKIE_CONSENT_ENABLED', true), /* * The name of the cookie in which we store if the user * has agreed to accept the conditions. */ 'cookie_name' => 'laravel_cookie_consent', /* * Set the cookie duration in days. Default is 365 * 20. */ 'cookie_lifetime' => 365 * 20, /* * Set the model class that represents the cookies table * Make sure your Cookie model implements the InteractsWithCookies trait */ 'cookie_class' => \App\Models\Cookie::class, /* * These middleware will get attached onto each Laravel Cookie Consent route, giving you * the chance to add your own middleware to this list or change any of * the existing middleware. Or, you can simply stick with this list. */ 'middleware' => ['web'], ];
使用
在您的应用程序布局中包含以下内容以渲染Cookie横幅
// before the closing body tag @include('cookie-consent::bar', ['text' => 'This website makes use of cookies', 'accept' => 'Accept', 'cancel' => 'Refuse'])
切换Cookie
作为开发者,您应提供一个页面,其中每个Cookie都以表格列表的形式呈现。为了让最终用户切换每个Cookie,请按照以下方式发送一个POST请求
@foreach ($cookies as $cookie) <!-- get the cookies from database --> <form action="{{ route('cookies.toggle', ['cookie' => $cookie]) }}" method="POST"> @csrf @if ($cookie->isEnabled()) <button type="submit" class="bg-green-200 text-green-600 text-xs rounded-full px-2 py-1">Active</button> @else <button type="submit" class="bg-red-200 text-red-600 text-xs rounded-full px-2 py-1">Not active</button> @endif </form> @endforeach
由软件包设置的端点cookies.toggle
将切换是否渲染Cookie。
样式
此软件包包含默认的Tailwind样式。如果您想自定义布局,应发布视图
php artisan vendor:publish --tag="laravel-cookie-consent-view"
现在您可以自己编辑布局。
插件加载的脚本期望按钮具有以下数据属性
<button data-refuse-cookies>Decline</button> <button data-accept-cookies>Accept</button>
安全性
如果您发现任何与安全相关的问题,请提出问题或直接联系我,邮箱地址为justijndepover@gmail.com。
贡献
如果您想对软件包进行任何更改或改进,请随时提出pull request。
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。