werxlab / wxlcookiebundle
WerxLab Cookie Bundle 用于处理欧洲隐私法
This package is not auto-updated.
Last update: 2024-10-02 07:02:56 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标准版就足够了)。然后将测试套件放在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/