mustangx / eu-cookie-law-bundle
Eu Cookie Law Bundle 处理新的隐私法
This package is not auto-updated.
Last update: 2020-11-02 09:18:11 UTC
README
注意: 此包是 XsolveCookieAcknowledgementBundle 的副本。由于 MustangX 项目 需要的一些小差异,我们将其分叉到我们自己的项目中。我们基于 Symfony3+ 构建,因此它可能在其他 Symfony 项目上也能工作。
此包提供了关于 Cookie 使用的信息,这是欧盟通过所谓的 欧盟 Cookie 法规 强制执行的。
它包括
- 快速且易于集成,学习曲线短
- 自动在页面底部注入 Cookie 条
- 手动注入 Cookie 条的能力(例如,对于 iframes)
- 更改文本和“关闭按钮”名称的能力
- 本地化意识
- 提供自己的 Cookie 条模板的能力
此包需要多字节字符串扩展。

安装
Symfony 4
需要预先安装
composer require templating
然后将其添加到 config/packages/framework.yaml
framework:
//...
templating:
engines: ['twig']
Symfony 3 & 4
1) 使用 composer 安装
composer require mustangx/eu-cookie-law-bundle
Symfony 3
2) 在 app/AppKernel.php 中运行包
public function registerBundles()
{
return array(
// ...
new Eu\CookieLawBundle\EuCookieLawBundle(),
);
}
Symfony 4
2) 在 `config/bundles.php` 中运行包
return [
//...
Eu\CookieLawBundle\EuCookieLawBundle::class => ['all' => true],
];
使用方法
对于所有静态页面
默认情况下,在启用包后,Cookie 条将在每个页面上可见。
对于 iframes
在 app/config/config.yml 中禁用响应注入
eu_cookie_law:
response_injection: false
在合适的位置包含 Cookie 条
{% include 'EuCookieLawBundle::cookie_law_bar.html.twig' %}
配置选项
配置可以在 app/config/config.yml 中进行调整
eu_cookie_law:
response_injection: true # default true
template: custom_cookie_bar.html.twig # twig template name, default: EuCookieLawBundle::cookie_law_bar.html.twig
更改 Cookie 条文本
将翻译文件放置在您的应用目录中
app/Resources/translations/EuCookieLawBundle.en.yml
并更改文本
cookie.message: My message
cookie.message.accept: Accept button text
当然,您可以在所需的任何区域设置中设置这些文本。
更改 Cookie 条外观
默认情况下,Cookie 条包含一些默认样式。如果您想更改这些样式,请使用 CSS。例如,您可能希望使用粉色背景并将其放置在页面顶部
#cookie-law-info-bar {
background: pink !important;
top: 50px !important;
bottom: auto !important;
}
请注意,每个样式都需要 `!important` 来覆盖提供的内联样式。
更改整个 Cookie 条模板
通过在配置中设置它(app/config/config.yml)使用您自己的模板
xsolve_cookie_acknowledgement:
template: ::custom_cookie_bar.html.twig
在上面的例子中,模板位于 app/Resources/custom_cookie_bar.html.twig
此外,基础模板可以由 Twig 扩展重用(使用了两个块:xsolve_cookie_message 和 xsolve_cookie_message_js),下面是示例
{% extends "EuCookieLawBundle::cookie_law_bar.html.twig" %}
{% block xsolve_cookie_message %}
{{ parent() }}
<div>This is something custom</div>
{% endblock %}
{% block xsolve_cookie_message_js %}
{{ parent() }}
<script type="text/javascript">
document.getElementById('js-cookie-law-close-button').onclick = function () {
alert('Got ya!');
}
</script>
{% endblock %}
自动测试
有一些简单的测试以确保一切正常工作。要运行测试,请将此包包含到某个 Symfony3 项目中(Symfony Standard Edition 就足够了)。然后将在 app/phpunit.xml 中的测试套件
<testsuite name="EU Cookie Law Bundle Suite">
<directory>../vendor/mustangx/eu-cookie-law-bundle/EU/CookieLawBundle/Tests/</directory>
</testsuite>
并运行
phpunit -c app/