该软件包向您的网站添加了一条简单的、可定制的 cookie 同意消息。当网站加载时,会出现横幅并让用户同意 cookie。一旦获得同意,横幅就会隐藏并保持隐藏状态。
这个包没有什么:
对于 Laravel 中更高级的 cookie 同意选项,请考虑这些替代方案。
我们投入了大量资源来创建一流的开源包。您可以通过购买我们的一款付费产品来支持我们。
我们非常感谢您从家乡寄给我们一张明信片,并注明您正在使用我们的哪种套餐。您可以在我们的联系页面上找到我们的地址。我们在虚拟明信片墙上发布所有收到的明信片。
您可以通过 Composer 安装该软件包:
composer require spatie/laravel-cookie-consent
该包将自动注册。
您可以选择发布配置文件:
php artisan vendor:publish --provider= " SpatieCookieConsentCookieConsentServiceProvider " --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
已设置,并且不会再次显示对话框
如果您想修改对话框中显示的文本,可以使用以下命令发布 lang 文件:
php artisan vendor:publish --provider= " SpatieCookieConsentCookieConsentServiceProvider " --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= " SpatieCookieConsentCookieConsentServiceProvider " --tag= " cookie-consent-views "
这会将index
和dialogContents
视图文件复制到resources/views/vendor/cookie-consent
。您可能只想修改dialogContents
视图。如果您需要修改此包的 JavaScript 代码,您可以在index
视图文件中进行操作。
您可以选择将SpatieCookieConsentCookieConsentMiddleware
添加到内核中,而不是在视图中包含cookie-consent::index
// app/Http/Kernel.php
class Kernel extends HttpKernel
{
protected $ middleware = [
// ...
Spatie CookieConsent CookieConsentMiddleware::class,
];
// ...
}
这会自动将cookie-consent::index
添加到响应内容的结束正文标记之前。
我们不是律师,无法提供法律建议。咨询法律专业人士哪些规则适用于您的项目。
请参阅变更日志以了解最近更改的更多信息。
composer test
详细信息请参阅贡献。
如果您发现任何与安全相关的问题,请发送电子邮件至 [email protected],而不是使用问题跟踪器。
麻省理工学院许可证 (MIT)。请参阅许可证文件以获取更多信息。