项目作者: ConnectHolland

项目描述 :
Symfony bundle to append Cookie Consent to your website to comply to AVG/GDPR for cookies.
高级语言: PHP
项目地址: git://github.com/ConnectHolland/cookie-consent-bundle.git
创建时间: 2019-01-31T15:49:16Z
项目社区:https://github.com/ConnectHolland/cookie-consent-bundle

开源协议:MIT License

下载


Scrutinizer Code Quality
Code Coverage
Build Status

Cookie Consent bundle for Symfony

Symfony bundle to append Cookie Consent to your website to comply to AVG/GDPR for cookies.

Installation

Step 1: Download using composer

In a Symfony application run this command to install and integrate Cookie Consent bundle in your application:

  1. composer require connectholland/cookie-consent-bundle

Step 2: Enable the bundle

When not using symfony flex, enable the bundle in the kernel manually:

  1. <?php
  2. // app/AppKernel.php
  3. public function registerBundles()
  4. {
  5. $bundles = array(
  6. // ...
  7. new ConnectHolland\CookieConsentBundle\CHCookieConsentBundle(),
  8. // ...
  9. );
  10. }

Step 3: Enable the routing

When not using symfony flex, enable the bundles routing manually:

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

Step 4: Configure to your needs

Configure your Cookie Consent with the following possible settings

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

Usage

Twig implementation

Load the cookie consent in Twig via render_esi ( to prevent caching ) at any place you like:

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

If you want to load the cookie consent with a specific locale you can pass the locale as a parameter:

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

Cookies

When a user submits the form the preferences are saved as cookies. The cookies have a lifetime of 1 year. The following cookies are saved:

  • Cookie_Consent: date of submit
  • Cookie_Consent_Key: Generated key as identifier to the submitted Cookie Consent of the user
  • CookieCategory[CATEGORY]: selected value of user (true or false)

Logging

AVG/GDPR requires all given cookie preferences of users to be explainable by the webmasters. For this we log all cookie preferences to the database. IP addresses are anonymized. This option can be disabled in the config.

Database logging

Themes

Dark Theme
Light Theme

TwigExtension

The following TwigExtension functions are available:

chcookieconsent_isCategoryAllowedByUser
check if user has given it’s permission for certain cookie categories

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

chcookieconsent_isCookieConsentSavedByUser
check if user has saved any cookie preferences

  1. {% if chcookieconsent_isCookieConsentSavedByUser() == true %}
  2. ...
  3. {% endif %}

Customization

Categories

You can add or remove any category by changing the config and making sure there are translations available for these categories.

Translations

All texts can be altered via Symfony translations by overwriting the CHCookieConsentBundle translation files.

Styling

CHCookieConsentBundle comes with a default styling. A sass file is available in Resources/assets/css/cookie_consent.scss and a build css file is available in Resources/public/css/cookie_consent.css. Colors can easily be adjusted by setting the variables available in the sass file.

To install these assets run:

  1. bin/console assets:install

And include the styling in your template:

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

Javascript

By loading Resources/public/js/cookie_consent.js the cookie consent will be submitted via ajax and the cookie consent will be shown on top of your website while pushing down the rest of the website.

Events

When a form button is clicked, the event of cookie-consent-form-submit-successful is created. Use the following code to listen to the event and add your custom functionality.

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

Template Themes

You can override the templates by placing templates inside your project (except for Symfony 5 projects):

  1. # app/Resources/CHCookieConsentBundle/views/cookie_consent.html.twig
  2. {% extends '@!CHCookieConsent/cookie_consent.html.twig' %}
  3. {% block title %}
  4. Your custom title
  5. {% endblock %}

Template override for Symfony 5 projects

You can override the templates by placing templaces inside you project as below. Be careful, it is important to place templates at this location: “app/templates/bundles/CHCookieConsentBundle/“ .

  1. # app/templates/bundles/CHCookieConsentBundle/cookie_consent.html.twig
  2. {% extends '@!CHCookieConsent/cookie_consent.html.twig' %}
  3. {% block intro %}
  4. Your custom intro
  5. {% endblock %}