fatalnetwork/symfony-cookie-consent-bundle

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

1.1.0 2023-03-31 10:14 UTC

This package is auto-updated.

Last update: 2024-08-30 01:23:45 UTC


README

Symfony 扩展包,用于将 Cookie 允许添加到您的网站,以符合 AVG/GDPR。此扩展包针对 Symfony 5 进行了测试。样式与 Bootstrap 5 兼容。

安装

步骤 1: 下载软件包

在 Symfony 应用程序中运行以下命令,将 Cookie 允许扩展包安装到您的应用程序中

composer require fatalnetwork/symfony-cookie-consent-bundle

在 Symfony 应用程序中运行以下命令,将 Bootstrap 安装到您的应用程序中

npm i bootstrap --save-dev

步骤 2: 配置

/config/bundles.php

将扩展包包含到您的项目中

return [
  // ...
  FatalNetwork\CookieConsentBundle\FNCookieConsentBundle::class => ['all' => true],
  // ...
];

/config/packages/fn_cookie_consent.yaml

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

fn_cookie_consent:
  categories: # Below are the default supported categories
    - 'analytics'
    - 'marketing'
    - 'preferences'
  use_logger: true # Logs user actions to database
  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
  disabled_routes: ['privacy', 'imprint'] # defined controller route names where cookieConsent will not be shown by default

/config/routes/fn_cookie_consent.yaml

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

fn_cookie_consent:
  resource: '@FNCookieConsentBundle/Resources/config/routing.yaml'

步骤 3: 资产

Bootstrap 样式

当在 webpack.config.js 中启用 SassLoader 时,请将来自 bootstrap 的 scss 文件添加到您的项目中

@import '~bootstrap/scss/bootstrap.scss';

Stimulus 控制器

将来自 Resources/assets/controllers/cookie_consent_modal_controller.js 的 stimulus 控制器复制到您的应用程序中的控制器文件夹(通常为 /assets/controllers/cookie_consent_modal_controller.js)

JS 和 CSS 重建

当设置新的 js 或样式(css 或 scss)文件时,您必须重建您公共资产中的新文件

npm run dev

步骤 4: 数据库

当使用记录器时,您必须创建一个迁移文件

php bin/console make:migration

之后,您可以迁移更改

php bin/console doctrine:migration:migrate

使用方法

Twig 实现

通过在您喜欢的任何位置使用 render_esi(以防止缓存)来加载 Cookie 允许

{{ render_esi(path('fn_cookie_consent.show', {
    route: app.request.attributes.get('_route')
})) }}
{{ render_esi(path('fn_cookie_consent.show_if_cookie_consent_not_set')) }}

如果您希望使用特定区域加载 Cookie 允许,可以传递区域作为参数

{{ render_esi(path('fn_cookie_consent.show', {
  locale: 'en',
  route: app.request.attributes.get('_route')
})) }}
{{ render_esi(path('fn_cookie_consent.show_if_cookie_consent_not_set', {
  locale: app.request.locale
})) }}

除了使用 render_esi(),您还可以使用 render() 函数

{{ render(path('fn_cookie_consent.show', {
    route: app.request.attributes.get('_route')
})) }}
{{ render(path('fn_cookie_consent.show_if_cookie_consent_not_set')) }}

Stimulus 实现

stimulus 控制器需要实现 eg. base.html.twig,请确保模态框位于此控制器内

<div id="root" {{ stimulus_controller('cookie_consent_modal') }}>
  {# ... #}
</div>

您可以设置更多打开模态框的链接,如下所示

<a href="#" {{ stimulus_action('cookie_consent_modal', 'show') }}>Cookies</a>

Cookie

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

  • Cookie_Consent: 提交日期
  • Cookie_Consent_Key: 作为用户提交的 Cookie 允许的标识符生成的密钥
  • CookieCategory[CATEGORY]: 用户的选定值(《true》或《false》)

记录

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

Log

主题

View

Twig 扩展

以下 Twig 扩展函数可用

fncookieconsent_isCategoryAllowedByUser 检查用户是否为特定 Cookie 类别授予了权限(返回 bool)

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

fncookieconsent_isCookieConsentSavedByUser 检查用户是否已保存任何 Cookie 偏好(返回 bool)

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

fncookieconsent_isCookieConsentOpenByDefault 检查模态视图是否必须为用户打开(返回 string)

{% if fncookieconsent_isCookieConsentOpenByDefault(string current_route, array disabled_routes) == 'true' %}
    ...
{% endif %}

自定义

类别

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

翻译

所有文本都可以通过覆盖 FNCookieConsentBundle 翻译文件来更改,以使用 Symfony 翻译。在语言文件中使用 privacy_route 变量来定义控制器中定义的路由名称。

样式

FNCookieConsentBundle 不自带样式,其样式来源于 Bootstrap。在 Resources/assets/css/cookie_consent.scss 中提供了一个 SASS 文件,在 Resources/public/css/cookie_consent.css 中提供了一个构建好的 CSS 文件,供您进行自定义。每个元素都有一个唯一的类名,因此您可以更改一切。

选项 SASS

将 Resources/assets/css/cookie_consent.scss 文件复制到您的项目样式文件夹中,例如 /assets/styles/cookie_consent.scss,并将此 scss 文件包含到您的 /assets/styles/app.scss 中。

@import './cookie_consent.scss';

选项 CSS

在模板中包含样式表,例如在 /templates/base.html.twig 中。

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

JavaScript

通过加载 Resources/public/js/cookie_consent.js,将通过 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/FNCookieConsentBundle/views/cookie_consent.html.twig
{% extends '@!FNCookieConsent/cookie_consent.html.twig' %}

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

Symfony 5 项目的模板覆盖

您可以通过在项目中放置以下位置的模板来覆盖模板。请注意,放置模板的位置非常重要:"app/templates/bundles/FNCookieConsentBundle/"。

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

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