aonodevs/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在页面底部提供一个浮动横幅。

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

拒绝按钮

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

自定义对话框文本

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

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视图文件中这样做。

使用中间件

您可以选择将Retinens\CookieConsent\CookieConsentMiddleware添加到您的kernel中,而不是在视图中包含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或将脚本添加到标题中。

注意

该法律对如何显示警告、哪些文本是必要的以及需要提供哪些选项的描述相当模糊。这个包将极大地帮助您遵守规定,但若想确保您的网站完全合规,您应该咨询法律专家。

变更日志

有关最近更改的详细信息,请参阅变更日志

测试

composer test

贡献

有关详细信息,请参阅贡献指南

安全

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

许可协议

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