kikwik/cookie-bundle

Symfony 5 的 Cookie 横幅

安装: 20

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 1

开放性问题: 0

类型:symfony-bundle

v1.0.7 2023-10-07 11:40 UTC

This package is auto-updated.

Last update: 2024-09-07 13:32:26 UTC


README

Symfony 5+ 的 Cookie 横幅

安装

打开命令行,进入项目目录,并执行以下命令以下载此包的最新稳定版本

$ composer require kikwik/cookie-bundle

更新数据库以创建 kw_cookie_consent_log 表

$ php bin/console make:migration
$ php bin/console doctrine:migrations:migrate

配置

创建 config/packages/kikwik_cookie.yaml 配置文件并清除缓存

kikwik_cookie:
    cookie_prefix:      'kwc_consent'   # the prefix for the cookie name (default is kwc_consent)
    cookie_lifetime:    180             # number days after cookie expiration (default is 6 months)
    consent_version:    '1.0'           # consent version (change to invalidate old consents)
    privacy_policy:     'app_privacy'   # route or url for privacy policy (default is null)
    cookie_policy:      'app_cookie'    # route or url for cookie policy (default is null)
    categories:         [ ]             # list of available categories, example: [ 'functional', 'analytics', 'profiling', 'marketing' ]
    enable_consent_log: false           # save user consent in database (default is false)
    banner_classes:
        wrapper: 'position-fixed bottom-0 start-0 end-0 p-1 border-top border-3 bg-white'
        actionWrapper: 'float-md-end text-center'
        btnAccept: 'btn btn-sm btn-success my-1'
        btnDeny: 'btn btn-sm btn-danger my-1'
        btnChoose: 'btn btn-sm btn-warning my-1'
        btnPrivacy: 'm-1'
        btnCookie: 'm-1'    
    

config/routes/kikwik_cookie.yaml 中导入路由包

kikwik_cookie_bundle:
    resource: '@KikwikCookieBundle/Resources/config/routes.xml'
    prefix: '/'

将翻译文件从 vendor/kikwik/cookie-bundle/src/Resources/translations/KikwikCookieBundle.xx.yaml 复制到 translations 目录以更改横幅文本

config/packages/twig.yaml 中创建一个 twig 全局变量以在模板中使用 ConsentManager

twig:
    globals:
        cookieConsentManager:   '@Kikwik\CookieBundle\Service\ConsentManager'
    {% if cookieConsentManager.categoryAllowed('analytics') %}
        <script>
            ...
        </script>
    {% endif %}