connectholland/cookie-consent-bundle

Symfony 扩展包,用于实现符合 AVG/GDPR 标准的 Cookie 允许。

安装次数 159,322

依赖项: 0

建议者: 0

安全: 0

星标: 18

关注者: 8

分支: 16

公开问题: 25

类型:symfony-bundle

1.0.0 2024-06-20 09:21 UTC

README

Scrutinizer Code Quality Code Coverage Build Status

Symfony 的 Cookie 允许扩展包

Symfony 扩展包,用于在网站上附加 Cookie 允许,以符合 AVG/GDPR 标准的 Cookie。

安装

步骤 1:使用 composer 下载

在 Symfony 应用程序中运行此命令以安装和整合 Cookie 允许扩展包到您的应用程序中

composer require connectholland/cookie-consent-bundle

步骤 2:启用扩展包

当不使用 symfony flex 时,手动在内核中启用扩展包

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new ConnectHolland\CookieConsentBundle\CHCookieConsentBundle(),
        // ...
    );
}

步骤 3:启用路由

当不使用 symfony flex 时,手动启用扩展包的路由

# app/config/routing.yml
ch_cookie_consent:
    resource: "@CHCookieConsentBundle/Resources/config/routing.yaml"

步骤 4:根据需要配置

使用以下可能的设置配置您的 Cookie 允许

ch_cookie_consent:
    theme: 'light' # light, dark
    categories: # Below are the default supported categories
        - 'analytics'
        - 'tracking'
        - 'marketing'
        - 'social_media'
    use_logger: true # Logs user actions to database
    position: 'top' # top, bottom
    simplified: false # When set to true the user can only deny or accept all cookies at once
    http_only: true # Sets HttpOnly on cookies
    form_action: $routeName # When set, xhr-Requests will only be sent to this route. Take care of having the route available.
    csrf_protection: true # The cookie consent form is csrf protected or not

使用方法

Twig 实现

通过 render_esi(防止缓存)在您喜欢的任何位置加载 cookie 允许

{{ render_esi(path('ch_cookie_consent.show')) }}
{{ render_esi(path('ch_cookie_consent.show_if_cookie_consent_not_set')) }}

如果您想使用特定的区域设置加载 cookie 允许,可以将区域设置作为参数传递

{{ render_esi(path('ch_cookie_consent.show', { 'locale' : 'en' })) }}
{{ render_esi(path('ch_cookie_consent.show_if_cookie_consent_not_set', { 'locale' : app.request.locale })) }}

Cookie

当用户提交表单时,偏好设置会保存为 cookie。这些 cookie 的有效期为 1 年。以下 cookie 被保存:

  • Cookie_Consent:提交日期
  • Cookie_Consent_Key:作为用户提交的 Cookie 允许的唯一标识符生成的密钥
  • Cookie_Category_[CATEGORY]:用户选择的价值(truefalse

日志记录

AVG/GDPR 要求网站管理员可以解释用户提供的所有 cookie 偏好。为此,我们将所有 cookie 偏好记录到数据库中。IP 地址被匿名化。此选项可以在配置中禁用。

Database logging

主题

Dark Theme Light Theme

Twig 扩展

以下 Twig 扩展函数可用:

chcookieconsent_isCategoryAllowedByUser 检查用户是否为特定 cookie 类别提供了权限

{% if chcookieconsent_isCategoryAllowedByUser('analytics') == true %}
    ...
{% endif %}

chcookieconsent_isCookieConsentSavedByUser 检查用户是否保存了任何 cookie 偏好

{% if chcookieconsent_isCookieConsentSavedByUser() == true %}
    ...
{% endif %}

自定义

类别

您可以通过更改配置并确保有这些类别的翻译来添加或删除任何类别。

翻译

所有文本都可以通过覆盖 CHCookieConsentBundle 翻译文件来更改。

样式

CHCookieConsentBundle 附带默认样式。在 Resources/assets/css/cookie_consent.scss 中有一个 sass 文件,在 Resources/public/css/cookie_consent.css 中有一个构建后的 css 文件。可以通过在 sass 文件中设置变量来轻松调整颜色。

要安装这些资源,请运行

bin/console assets:install

并将样式包含在您的模板中

{% include "@CHCookieConsent/cookie_consent_styling.html.twig" %}

JavaScript

通过加载 Resources/public/js/cookie_consent.js,cookie 允许将通过 AJAX 提交,并且 cookie 允许将显示在您网站顶部,同时将其他所有内容向下推。

事件

当表单按钮被点击时,会创建 cookie-consent-form-submit-successful 事件。使用以下代码来监听事件并添加您自己的功能。

document.addEventListener('cookie-consent-form-submit-successful', function (e) {
    // ... your functionality
    // ... e.detail is available to see which button is clicked.
}, false);

模板主题

您可以通过在项目中放置模板(除了 Symfony 5 项目)来覆盖模板。

# app/Resources/CHCookieConsentBundle/views/cookie_consent.html.twig
{% extends '@!CHCookieConsent/cookie_consent.html.twig' %}

{% block title %}
    Your custom title
{% endblock %}

Symfony 5 项目的模板覆盖

您可以通过在项目中放置模板来覆盖模板,如下所示。请注意,将模板放置在此位置非常重要:“app/templates/bundles/CHCookieConsentBundle/”。

# app/templates/bundles/CHCookieConsentBundle/cookie_consent.html.twig
{% extends '@!CHCookieConsent/cookie_consent.html.twig' %}

{% block intro %}
    Your custom intro
{% endblock %}