dmstr / yii2-cookie-consent
Yii2 Cookie Consent Widget
1.4.4
2020-04-24 10:39 UTC
Requires
- npm-asset/dmstr--cookie-consent: ^0.4.0
- yiisoft/yii2: ~2.0.0
README
欧盟Cookie法规的解决方案
入门
安装
composer require dmstr/yii2-cookie-consent
CookieConsentHelper 组件
yii 配置
'components' => [ 'cookieConsentHelper' => [ 'class' => dmstr\cookieconsent\components\CookieConsentHelper::class ] ]
PHP 中使用
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') ] ] ] ] ]) ?>
<?php if (\Yii::$app->cookieConsentHelper->hasConsent('statistics')): ?> <!-- Google Analytics Script--> <?php endif; ?>
使用 TWIG
{{ 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 %}
选项
切换弹出窗口、控件和详情
<button class="cookie-consent-open">open</button> <button class="cookie-consent-close">close</button> <button class="cookie-consent-toggle">toggle</button> <button class="cookie-consent-controls-open">Open controls</button> <button class="cookie-consent-controls-close">Close controls</button> <button class="cookie-consent-controls-toggle">Toggle controls</button> <button class="cookie-consent-details-open">Open Details</button> <button class="cookie-consent-details-close">Close Details</button> <button class="cookie-consent-details-toggle">Toggle details</button>
CSS 示例
.cookie-consent-popup { animation-name: show; animation-duration: 1s; animation-timing-function: ease; display: none; position: fixed; bottom: 0; left: 0; width: 100%; z-index: 999999; } .cookie-consent-popup.open { display: block; opacity: 1; animation-name: show; animation-duration: 1s; animation-timing-function: ease; } .cookie-consent-controls { max-height: 0; overflow: hidden; -webkit-transition: max-height 0.5s ease-out; -moz-transition: max-height 0.5s ease-out; transition: max-height 0.5s ease-out; } .cookie-consent-controls.open { margin: 0 0 30px 0; max-height: 600px; } .cookie-consent-details { max-height: 0; overflow: hidden; -webkit-transition: max-height 0.5s ease-out; -moz-transition: max-height 0.5s ease-out; transition: max-height 0.5s ease-out; } .cookie-consent-details.open { max-height: 600px; } @keyframes show { from {opacity: 0;} to {opacity: 1;} } @keyframes hide { from {opacity: 1;} to {opacity: 0;} }
设置配置示例 phemellc/yii2-settings
- 部分:cookie-consent
- 键:config
- 类型:对象
{ "name": "cookie_consent_status", "path": "/", "domain": "", "expiryDays": 365, "message": "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": "Speichern", "learnMore": "Datenschutzerklärung", "link": "#", "consent": { "necessary": { "label": "Necessary", "checked": true, "disabled": true }, "statistics": { "label": "Statistics", "cookies": [ { "name": "_ga" }, { "name": "_gat", "domain": "", "path": "/" }, { "name": "_gid", "domain": "", "path": "/" } ] }, "0": "marketing", "1": "external-media" } }
<?php $config = Yii::$app->settings->get('config', 'cookie-consent', []); $config = isset($config->scalar) ? $config->scalar : '{}'; $config = json_decode($config, true); ?> <?= CookieConsent::widget($config) ?>
值得了解
如果你定义了一个无效的cookie consent helper组件,小部件会抛出 yii\base\InvalidConfigException
。