albertborsos/yii2-gdpr-cookie-consent

GDPR 兼容的 Cookie Consent 小部件允许用户选择他们想要接受的 cookie 类型。

安装量: 22,743

依赖项: 1

建议者: 0

安全: 0

星标: 11

关注者: 8

分支: 7

开放问题: 3

类型:yii2-extension

1.3.0 2021-11-02 13:10 UTC

README

GDPR 兼容的 Cookie Consent 小部件允许用户选择他们想要接受的 cookie 类型。

Build Status

安装

安装此扩展的首选方法是通过 composer

运行

composer require --prefer-dist albertborsos/yii2-gdpr-cookie-consent

使用方法

将组件添加到您的配置文件中

<?php
return [
    // ...
    'components' => [
        // ...
        'cookieConsent' => [
            'class' => \albertborsos\cookieconsent\Component::class,
            'urlSettings' => ['/site/cookie-settings'],
            'urlPrivacyPolicy' => ['/site/privacy-policy'],
            'documents' => [
                ['name' => 'Privacy Policy', 'url' => ['/docs/privacy-policy.pdf']],
            ],
            'disabledCategories' => [
                \albertborsos\cookieconsent\helpers\CookieHelper::CATEGORY_BEHAVIOR,
            ],
        ],
        // ...
        'i18n' => [
            // ...
            'translations' => [
                'cookieconsent/*' => [
                    'class' => 'yii\i18n\PhpMessageSource',
                    'basePath' => '@vendor/albertborsos/yii2-gdpr-cookie-consent/src/messages',
                ],
            ],
            // ...
        ],
    ],
    // ...
];

在布局中注册小部件。例如,在一个 _cookieconsent.php 部分视图中。

<?php
/** @var \albertborsos\cookieconsent\Component $component */
$component = Yii::$app->cookieConsent;
$component->registerWidget([
    'policyLink' => ['/default/cookie-settings'],
    'policyLinkText' => \yii\helpers\Html::tag('i', null, ['class' => 'fa fa-cog']) . ' Beállítások',
    'pluginOptions' => [
        'expiryDays' => 365,
        'hasTransition' => false,
        'revokeBtn' => '<div class="cc-revoke {{classes}}">Cookie Policy</div>',
    ],
]);

将 cookie 设置表单添加到您的任何控制器中

<?php

namespace app\controllers;

class SiteController extends \yii\web\Controller
{
    public function actions()
    {
        return [
            'cookie-settings' => \albertborsos\cookieconsent\actions\CookieSettingsAction::class,
            'privacy-policy' => \albertborsos\cookieconsent\actions\PrivacyPolicyAction::class,
        ];
    }
}

使用以下方式检查用户是否允许使用相关小部件:CookieConsent 辅助类

<?php

use \albertborsos\cookieconsent\helpers\CookieHelper;
use \albertborsos\cookieconsent\Component;

if(CookieHelper::isAllowedType(CookieHelper::TYPE_GOOGLE_ANALYTICS)){
    // register GA script
}

if(CookieHelper::isAllowedCategory(CookieHelper::CATEGORY_BEHAVIOR)){
    // register hotjar script
}