spatie / laravel-cookie-consent
使您的Laravel应用程序符合疯狂的欧盟cookie法规
Requires
- php: ^8.2
- illuminate/cookie: ^10.0|^11.0
- illuminate/support: ^10.0|^11.0
- illuminate/view: ^10.0|^11.0
- spatie/laravel-package-tools: ^1.9
Requires (Dev)
- fakerphp/faker: ^1.9
- orchestra/testbench: ^8.0|^9.0
- pestphp/pest: ^2.34
- dev-main
- 3.3.2
- 3.3.1
- 3.3.0
- 3.2.4
- 3.2.3
- 3.2.2
- 3.2.1
- 3.2.0
- 3.1.2
- 3.1.1
- 3.1.0
- 3.0.1
- 3.0.0
- v2.x-dev
- 2.12.13
- 2.12.12
- 2.12.11
- 2.12.10
- 2.12.9
- 2.12.8
- 2.12.7
- 2.12.6
- 2.12.5
- 2.12.4
- 2.12.3
- 2.12.2
- 2.12.1
- 2.12.0
- 2.11.0
- 2.10.0
- 2.9.0
- 2.8.0
- 2.7.0
- 2.6.3
- 2.6.2
- 2.6.1
- 2.6.0
- 2.5.0
- 2.4.1
- 2.4.0
- 2.3.0
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.0
- 2.0.0
- v1.x-dev
- 1.8.0
- 1.7.0
- 1.6.1
- 1.6.0
- 1.5.0
- 1.4.1
- 1.4.0
- 1.3.0
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.0
- 1.0.1
- 1.0.0
- 0.0.1
- dev-update-readme
This package is auto-updated.
Last update: 2024-09-20 13:42:12 UTC
README
此包为您网站添加了一个简单、可定制的cookie同意信息。当网站加载时,横幅出现并允许用户同意cookie。一旦同意,横幅将隐藏并保持隐藏。
此包不包含的内容
- 包含“拒绝”所有cookie的选项,这可能是必需的。
- 在同意之前阻止跟踪器和cookie。您需要自己处理。
- 包含不同同意类别的选项,如“必需”或“营销”。
在Laravel中需要更高级的cookie同意选项,请考虑以下替代方案。
支持我们
我们投入了大量资源来创建一流的开放源代码包。您可以通过购买我们的付费产品之一来支持我们。
我们非常感谢您从家乡寄给我们明信片,并说明您正在使用我们的哪些包。您可以在我们的联系页面上找到我们的地址。我们将发布所有收到的明信片在我们的虚拟明信片墙上。
安装
您可以通过composer安装此包
composer require spatie/laravel-cookie-consent
包将自动注册自己。
可选地,您可以发布配置文件
php artisan vendor:publish --provider="Spatie\CookieConsent\CookieConsentServiceProvider" --tag="cookie-consent-config"
这是已发布配置文件的内容
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, ];
cookie域通过config/session.php中的'domain'键设置,请确保您在.env中为SESSION_DOMAIN添加一个值。如果您正在使用带有url中端口的域名,例如'localhost:3000',则在此之前此包将无法正常工作。
用法
要显示对话框,您只需在模板中包含此视图即可
//in your blade template @include('cookie-consent::index')
这将渲染以下对话框,当样式化时,将非常类似于这个。
此包提供的默认样式使用TailwindCSS v2在页面底部提供浮动横幅。
当用户点击“允许cookie”时,将设置一个laravel_cookie_consent
cookie,并将对话框从DOM中删除。在下一次请求中,Laravel将注意到已设置laravel_cookie_consent
,并将不再显示对话框。
自定义对话框文本
如果您想修改对话框中显示的文本,可以使用此命令发布语言文件
php artisan vendor:publish --provider="Spatie\CookieConsent\CookieConsentServiceProvider" --tag="cookie-consent-translations"
这将把此文件发布到resources/lang/vendor/cookie-consent/en/texts.php
。
return [ 'message' => 'Please be informed that this site uses cookies.', 'agree' => 'Allow cookies', ];
如果您想将这些值翻译成,例如,法语,只需将此文件复制到resources/lang/vendor/cookie-consent/fr/texts.php
并填写法语翻译。
自定义对话框内容
如果您需要完全控制对话框的内容,可以发布包的视图
php artisan vendor:publish --provider="Spatie\CookieConsent\CookieConsentServiceProvider" --tag="cookie-consent-views"
这将把index
和dialogContents
视图文件复制到resources/views/vendor/cookie-consent
。您可能只想修改dialogContents
视图。如果您需要修改此包的JavaScript代码,可以在index
视图文件中这样做。
使用中间件
您可以选择不在视图中包含 cookie-consent::index
,而是将 Spatie\CookieConsent\CookieConsentMiddleware
添加到您的内核中
// app/Http/Kernel.php class Kernel extends HttpKernel { protected $middleware = [ // ... \Spatie\CookieConsent\CookieConsentMiddleware::class, ]; // ... }
这将自动在关闭 body 标签之前将 cookie-consent::index
添加到响应的内容中。
注意
我们不是律师,无法提供法律建议。请咨询法律专业人士了解您项目适用的规则。
更新日志
有关最近更改的更多信息,请参阅更新日志。
测试
composer test
贡献
有关详细信息,请参阅贡献指南。
安全
如果您发现任何安全相关的问题,请通过电子邮件freek@spatie.be联系,而不是使用问题跟踪器。
致谢
许可协议
MIT 许可协议(MIT)。有关更多信息,请参阅许可文件。