PHP code example of dmstr / yii2-cookie-consent

1. Go to this page and download the library: Download dmstr/yii2-cookie-consent library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dmstr / yii2-cookie-consent example snippets


'components' => [
    'cookieConsentHelper' => [
        'class' => dmstr\cookieconsent\components\CookieConsentHelper::class
    ]
]

use dmstr\cookieconsent\widgets\CookieConsent;
<?= CookieConsent::widget([
    'name' => 'cookie_consent_status',
    'path' => '/',
    'domain' => '',
    'expiryDays' => 365,
    'message' => Yii::t('cookie-consent', 'We use cookies to ensure the proper functioning of our website. For an improved visit experience we use analysis products. These are used when you agree with "Statistics".'),
    'save' => Yii::t('cookie-consent', 'Save'),
    'acceptAll' => Yii::t('cookie-consent', 'Accept all'),
    'controlsOpen' => Yii::t('cookie-consent', 'Change'),
    'detailsOpen' => Yii::t('cookie-consent', 'Cookie Details'),
    'learnMore' => Yii::t('cookie-consent', 'Privacy statement'),
    'visibleControls' => true,
    'visibleDetails' => false,
    'link' => '#',
    'consent' => [
        'necessary' => [
            'label' => Yii::t('cookie-consent', 'Necessary'),
            'checked' => true,
            'disabled' => true
        ],
        'statistics' => [
            'label' => Yii::t('cookie-consent', 'Statistics'),
            'cookies' => [
                ['name' => '_ga'],
                ['name' => '_gat', 'domain' => '', 'path' => '/'],
                ['name' => '_gid', 'domain' => '', 'path' => '/']
            ],
            'details' => [
                [
                    'title' => Yii::t('cookie-consent', 'Google Analytics'),
                    'description' => Yii::t('cookie-consent', 'Create statistics data')

                ],
                [
                    'title' => Yii::t('cookie-consent', 'Goal'),
                    'description' => Yii::t('cookie-consent', '_ga, _gat, _gid, _gali')

                ]
            ]
        ]
    ]
]) 

 if (\Yii::$app->cookieConsentHelper->hasConsent('statistics')): 

{{ use('dmstr/cookieconsent/widgets/CookieConsent') }}
{{ CookieConsent_widget({
    "name": "cookie_consent_status",
    "path": "/",
    "domain": "",
    "expiryDays": 365,
    "message": t("cookie-consent", "We use cookies to ensure the proper functioning of our website. For an improved visit experience we use analysis products. These are used when you agree with 'Statistics'."),
    "save": t("cookie-consent", "Save"),
    "acceptAll": t("cookie-consent", "Accept all"),
    "controlsOpen": t("cookie-consent", "Change"),
    "detailsOpen": t("cookie-consent", "Cookie Details"),
    "learnMore": t("cookie-consent", "Privacy statement"),
    "visibleControls": false,
    "visibleDetails": false,
    "link": "#",
    "consent": {
        "necessary": {
            "label": t("cookie-consent", "Necessary"),
            "checked": true,
            "disabled": true
        },
        "statistics": {
            "label": t("cookie-consent", "Statistics"),
            "cookies": [
                {"name": "_ga", "domain": "", "path": "/"},
                {"name": "_gat", "domain": "", "path": "/"},
                {"name": "_gid", "domain": "", "path": "/"},
                {"name": "_gali", "domain": "", "path": "/"}
            ],
            "details": [
                {
                    "title": t("cookie-consent", "Google Analytics"),
                    "description": t("cookie-consent", "Create statistics data")
                },
                {
                    "title": t("cookie-consent", "Goal"),
                    "description": t("cookie-consent", "_ga, _gat, _gid, _gali")
                
                }
            ]
        }
    }
}) }}

{% if app.cookieConsentHelper.hasConsent('statistics') %}
    {# Google Analytics Code #}
{% endif %}


    $config = Yii::$app->settings->get('config', 'cookie-consent', []);
    $config = isset($config->scalar) ? $config->scalar : '{}';
    $config = json_decode($config, true);