ideacatlab / laravel-cookie-consent-enhanced
使您的Laravel应用程序符合欧盟的cookie法规,并实施一个允许用户管理自己的cookie偏好的cookie控制系统
Requires
- php: ^8.2
- illuminate/cookie: ^10.23.1
- illuminate/support: ^10.23.1
- illuminate/view: ^10.23.1
- spatie/laravel-package-tools: ^1.16.1
Requires (Dev)
- fakerphp/faker: ^1.23.0
- orchestra/testbench: ^8.10.2
- pestphp/pest: ^2.18.2
README
使用增强的cookie同意来提高Laravel应用程序的GDPR合规性
确保符合针对欧盟公民针对或拥有的网站的cookie使用法规至关重要。深入了解这个Laravel包的功能,这是一个由Spatie的Laravel Cookie Consent派生出的强大扩展。由布加勒斯特的IdeaCat网络开发公司开发,这个包不仅简化了cookie同意对话框的实现,还扩展了其功能。除了基本功能之外,它还引入了高级功能,如预定义数据库表、用户数据管理的行政工具以及无缝集成GDPR合规性要求。
有关欧盟关于cookie的立法的更多详细信息,请在此处。
安装和配置
1. 安装包
您可以通过Composer轻松安装此包。打开您的终端并运行
composer require ideacatlab/laravel-cookie-consent-enhanced
该包将自动在您的Laravel应用程序中注册自己。
2. 发布供应商文件
运行发布命令以发布视图、语言、数据库迁移、配置和资产。
php artisan vendor:publish --provider="Ideacatlab\LaravelCookieConsentEnhanced\CookieConsentServiceProvider"
3. 迁移数据库
执行迁移命令以创建所需的数据库表
php artisan migrate
配置细节
在发布配置文件后,您会在config目录中找到一个名为cookie-consent-enhanced.php
的文件。以下是配置选项概述
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' => 'cookie_consent', /* * Set the cookie duration in days. Default is 365 * 20. */ 'cookie_lifetime' => 365 * 20, ];
关于cookie域的注意事项
请确保通过在config/session.php
文件中使用'domain'键来设置cookie域。在.env
文件中将相应值添加到SESSION_DOMAIN
。如果您的URL包含端口号,例如'localhost:3000',则设置此值对于包正确运行至关重要。
用法和定制
使用Blade模板显示cookie同意对话框
要轻松地在模板中显示cookie同意对话框,请在Blade文件中包含以下行
@include('cookie-consent-enhanced::index')
这将渲染一个类似于下面所示的对话框。请注意,由TailwindCSS v2提供的默认样式在页面底部创建了一个浮动横幅。
使用中间件显示cookie同意对话框
不要手动在视图中包含cookie-consent-enhanced::index
,请考虑将中间件添加到您的kernel中
// app/Http/Kernel.php class Kernel extends HttpKernel { protected $middleware = [ // ... \Ideacatlab\LaravelCookieConsentEnhanced\CookieConsentMiddleware::class, ]; // ... }
这将自动在关闭body标签之前将cookie-consent-enhanced::index
添加到您的响应中。
单击“允许cookie”后,将设置一个cookie_consent
cookie,并从DOM中删除对话框。Laravel在后续请求中识别到设置的laravel_cookie_consent
cookie,将不再显示对话框。
自定义对话框文本
文件发布到resources/lang/vendor/cookie-consent-enhanced/en/texts.php
。然后您可以根据以下方式自定义文本
return [ 'message' => 'Please be informed that this site uses cookies.', 'agree' => 'Allow cookies', ];
对于翻译,将文件复制到,例如resources/lang/vendor/cookie-consent-enhanced/fr/texts.php
并提供法语翻译。
自定义对话框内容
文件已发布到 index
和 dialogContents
视图文件,存放在 resources/views/vendor/cookie-consent-enhanced
。通常情况下,您只需要修改 dialogContents
视图。在 index
视图文件中也可以调整 JavaScript 代码。
测试
运行以下命令以执行测试
composer test
安全性
如果您发现任何与安全相关的问题,请通过 razvan@ideacat.ro 联系我们,而不是使用问题跟踪器。
许可证
本项目遵循 MIT 许可证。有关更多信息,请参阅 许可证文件。