retinens/laravel-cookie-consent

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

4.1.2 2021-06-15 11:16 UTC

README

Latest Version on Packagist Software License GitHub Workflow Status Total Downloads

所有属于欧盟公民或针对欧盟公民的网站都必须遵守一项疯狂的欧盟法律。这项法律要求显示一个对话框,向用户说明您的网站如何使用cookie。您可以在欧洲委员会网站上了解更多关于该法规的信息:欧洲委员会网站

用户应提供选择同意或不同意可选cookie的选项。

此包基于Spatie提供的spatie/laravel-cookie-consent包。

此包提供了一个易于配置的视图来显示消息。还包括JavaScript代码,用于在用户同意或不同意cookie策略时设置cookie。用户也可以不同意,并将cookie设置为0。当设置了该cookie时,包将不会显示对话框。

安装

您可以通过composer安装此包

composer require retinens/laravel-cookie-consent

包将自动注册自身。

可选地,您可以发布配置文件

php artisan vendor:publish --provider="Retinens\CookieConsent\CookieConsentServiceProvider" --tag="cookie-consent-config"

这是已发布的配置文件的内容

return [

    /*
     * Use this setting to enable the cookie consent dialog.
     */
    'enabled' => env('COOKIE_CONSENT_ENABLED', true),
    
    /*
     * Use this setting to add a refuse button on the dialog.
    */
    'refuse_enabled' => env('COOKIE_CONSENT_REFUSE_ENABLED', false),

    /*
     * 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 * 1.
     */
    'cookie_lifetime' => 365 * 1,
];

使用方法

要显示对话框,只需将此视图包含在您的模板中即可

//in your blade template
@include('cookie-consent::index')

这将渲染以下对话框,当样式化后,将非常像这个。

dialog

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

当用户点击“允许cookies”时,将设置一个laravel_cookie_consent cookie,并将对话框从DOM中移除。在下一次请求中,Laravel将注意到已设置了laravel_cookie_consent,因此不会再次显示对话框

拒绝按钮

如果您想在对话框中添加拒绝按钮,可以在配置文件中启用该选项。当用户点击“拒绝非必需cookie”时,将设置一个值为0laravel_cookie_consent cookie。

自定义对话框文本

如果您想修改对话框中显示的文本,可以使用此命令发布lang文件

php artisan vendor:publish --provider="Retinens\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',
    'refuse' => 'Refuse non-essential cookies',
];

如果您想将值翻译成,例如,法语,只需将此文件复制到resources/lang/vendor/cookie-consent/fr/texts.php并填写法语翻译。

自定义对话框内容

如果您需要完全控制对话框的内容,可以发布包的视图

php artisan vendor:publish --provider="Retinens\CookieConsent\CookieConsentServiceProvider" --tag="cookie-consent-views"

这将把indexdialogContents视图文件复制到resources/views/vendor/cookie-consent。您可能只需要修改dialogContents视图。如果您需要修改此包的JavaScript代码,可以在index视图文件中这样做。

使用中间件

您可以选择在kernel中添加Retinens\CookieConsent\CookieConsentMiddleware而不是在视图中包含cookie-consent::index

// app/Http/Kernel.php

class Kernel extends HttpKernel
{
    protected $middleware = [
        // ...
        \Retinens\CookieConsent\CookieConsentMiddleware::class,
    ];

    // ...
}

这将自动将cookie-consent::index添加到响应内容中,紧接在关闭body标签之前。

助手

在您的代码中,您可以使用外观来获取用户是否已接受或未接受非必需cookie的信息。

CookieConsent::hasConsented();
CookieConsent::hasRefused();

这样,您可以选择为用户添加cookie或将脚本添加到标题中。

注意

关于如何显示警告、哪些文本是必要的以及需要提供哪些选项,该法规表述得相当模糊。这个包将有助于合规,但如果你想要100%确信你的网站没问题,你应该咨询法律专家。

变更日志

请参阅变更日志获取最近更改的更多信息。

测试

composer test

贡献

请参阅贡献指南以获取详细信息。

安全

如果你发现任何安全相关的问题,请通过电子邮件lucas@retinens.com联系,而不是使用问题追踪器。

许可

MIT许可(MIT)。有关更多信息,请参阅许可文件