connectholland / cookie-consent-bundle
Symfony 扩展包,用于实现符合 AVG/GDPR 标准的 Cookie 允许。
Requires
- php: ^7.2 || ^8.0
- doctrine/annotations: ^1.6|^2.0
- doctrine/doctrine-bundle: ^1.10 || ^2.0
- doctrine/orm: ^2.5
- symfony/asset: ^3.4 || ^4.2 || ^5.0 || ^6.0
- symfony/dependency-injection: ^3.4 || ^4.2 || ^5.0 || ^6.0
- symfony/doctrine-bridge: ^3.4 || ^4.2 || ^5.0 || ^6.0
- symfony/expression-language: ^3.4 || ^4.2 || ^5.0 || ^6.0
- symfony/form: ^3.4 || ^4.2 || ^5.0 || ^6.0
- symfony/framework-bundle: ^3.4 || ^4.2 || ^5.0 || ^6.0
- symfony/http-foundation: ^3.4 || ^4.2 || ^5.0 || ^6.0
- symfony/templating: ^3.4 || ^4.2 || ^5.0 || ^6.0
- symfony/translation: ^3.4 || ^4.2 || ^5.0 || ^6.0
- symfony/twig-bridge: ^3.4 || ^4.2 || ^5.0 || ^6.0
- symfony/twig-bundle: ^3.4 || ^4.2 || ^5.0 || ^6.0
- twig/twig: ^2.6 || ^3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.14
- phpunit/phpunit: ^7.5 || ^8.5 || ^9.5
- symfony/yaml: ^3.4 || ^4.2 || ^5.0 || ^6.0
- dev-master
- 1.0.0
- 0.12.0
- 0.11.0
- 0.10.0
- 0.9.11
- 0.9.10
- 0.9.9
- 0.9.8
- 0.9.7
- 0.9.6
- 0.9.5
- 0.9.4
- 0.9.3
- 0.9.2
- 0.9.1
- 0.9.0
- 0.8.7
- 0.8.6
- 0.8.5
- 0.8.4
- 0.8.3
- 0.8.2
- 0.8.1
- 0.8.0
- 0.7.1
- 0.7.0
- 0.6.3
- 0.6.2
- 0.6.1
- 0.6.0
- 0.5.0
- 0.4.2
- 0.4.1
- 0.4.0
- 0.3.0
- 0.2.0
- 0.1.3
- 0.1.2
- 0.1.1
- 0.1.0
- dev-fix-css-build
- dev-feature/http-only
- dev-fix/simplified-true
- dev-readme-typo-fix
This package is auto-updated.
Last update: 2024-09-20 09:54:38 UTC
README
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]:用户选择的价值(true 或 false)
日志记录
AVG/GDPR 要求网站管理员可以解释用户提供的所有 cookie 偏好。为此,我们将所有 cookie 偏好记录到数据库中。IP 地址被匿名化。此选项可以在配置中禁用。
主题
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 %}