spatie/laravel-cookie-consent

使您的Laravel应用程序符合疯狂的欧盟cookie法规

3.3.2 2024-08-07 11:06 UTC

README

Latest Version on Packagist Software License run-tests Total Downloads

此包为您网站添加了一个简单、可定制的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')

这将渲染以下对话框,当样式化时,将非常类似于这个。

dialog

此包提供的默认样式使用TailwindCSS v2在页面底部提供浮动横幅。

当用户点击“允许cookie”时,将设置一个laravel_cookie_consentcookie,并将对话框从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"

这将把indexdialogContents视图文件复制到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)。有关更多信息,请参阅许可文件