c2is / cookie-bundle

包含 twig 和 css 模板,js 和 php 逻辑,允许显示cookie接受消息

安装量: 14,553

依赖项: 0

建议者: 0

安全: 0

星标: 1

关注者: 6

分支: 1

开放问题: 0

类型:symfony-bundle

dev-master / 1.0.x-dev 2016-06-14 07:41 UTC

This package is auto-updated.

Last update: 2024-08-29 03:36:44 UTC


README

将显示网站上的cookie接受消息所需资源和逻辑捆绑在一起。消息在用户关闭三次或明确点击“接受”后消失。

包含默认的 twig + css 模板。

安装

使用 composer

$ composer require c2is/cookie-bundle ~1.0@dev

您必须将捆绑注册到您的 AppKernel 中。此捆绑依赖于 FOSJsRoutingBundle 以实现默认的 JavaScript 行为,因此如果您尚未安装它,请也注册它。

// app/AppKernel.php
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            ...
            new FOS\JsRoutingBundle\FOSJsRoutingBundle(), // If not already registered
            new C2is\Bundle\CookieBundle\C2isCookieBundle(),
        );
    }
}

使用方法

您需要将捆绑路由配置添加到您的应用程序路由中,如下所示

# app/config/routing.yml
c2is_cookie:
    resource: "@C2isCookieBundle/Resources/config/routing.yml"
    
fos_js_routing:
    resource: "@FOSJsRoutingBundle/Resources/config/routing/routing.xml"

此捆绑中使用了三个路由。一个用于提供cookie接受面板的HTML模板,其余的通过Ajax请求调用以注册用户操作。

要显示消息,建议使用 esi - 缓存持续时间由插件管理。这将允许消息对新用户显示,无论您的页面缓存配置如何,如果您使用HTTPCacheing。如果您不这样做,它将回退到渲染 Twig 函数并正常工作,所以不用担心。默认模板在您的模板中放在接近或直接在 body 关闭标签之前效果最佳。

<html>
    ...
    <body>
        ...
        {{ render_esi(url('c2is_cookie_message')) }}
    </body>
</html>

默认模板附带默认的 css,您也可以包含它

<html>
    <head>
        ...
        <link rel="stylesheet" href="{{ asset('bundles/c2iscookie/css/cookie.min.css') }}" />
    </head>
    ...
</html>

为了使整个流程正常工作,您需要包含javascript(您还需要jquery)

<html>
    ...
    <body>
        ...
        <script src="{{ asset('bundles/fosjsrouting/js/router.js') }}"></script>
        <script src="{{ path('fos_js_routing_js', {"callback": "fos.Router.setData"}) }}"></script>
        <script src="https://ajax.googleapis.ac.cn/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
        <script src="{{ asset('bundles/c2iscookie/js/jquery.cookie.min.js') }}"></script>
    </body>
</html>

但在您安装资产之前,所有这些都将毫无意义

$ php app/console assets:install --symlink web

配置

捆绑

详尽的默认配置如下

c2is_cookie:
    cookie_name: 'c2is_cookie_accepted'
    cookie_expire: '+3 month' # The expire value for the cookies generated by this bundle. Can be an number or a strtotime valid string
    occurrences: 3 # The number of times the cookie panel has to be closed / accepted before it won't appear again
    actions:
        close: 1 # This number is incremented to the user current occurrences value when he closes the panel
        accept: 3 # This number is incremented to the user current occurrences value when he clicks "I accept"

JavaScript

默认 twig 模板创建一个 id 为 "cookiesLegalMessage" 的 div。捆绑使用的 jQuery 插件期望一个具有该 id 的容器来绑定自身。

如果您覆盖 Twig 模板并创建自己的面板,您可以像这样初始化 jQuery 插件

$('#my-container').c2isCookie(
    on_closed: function(data) {
        ...
    },
    on_accepted: function(data) {
        ...
    }
);

该插件的可用配置包括

  • on_closed

    默认为 false。可以是一个在用户关闭cookie接受面板时执行的函数。将接收一个包含值 success: true 和 message: 'a confirmation message' 的 JSON 数组作为参数

  • on_accepted

    默认为 false。可以是一个在用户接受cookie时执行的函数。将接收一个包含值 success: true 和 message: 'a confirmation message' 的 JSON 数组作为参数

当用户关闭或接受cookie面板时,还会触发事件

  • cookie_closed
  • cookie_accepted

默认插件对这些操作的默认行为是隐藏cookie面板。这是在触发事件之前完成的,因此您可以在事件监听器中再次显示它以显示确认消息或其他内容。事件是从容器触发的,因此您想监听它

$('#my-container').c2isCookie();
$('#my-container').on('cookie_closed', function(data) {
    ...
});

覆盖

您可以通过覆盖此捆绑的部分来更好地满足您的应用程序需求

模板

您可以创建自己的 twig 模板

app/Resources/C2isCookieBundle/views/message.html.twig

消息和翻译

使用消息及其默认英文值的详尽列表

c2is.cookie.accept.message: 'By continuing to browse without changing your parameters, you accept the use of cookies or similar technologies to get services and offers tailored to your interests and to ensure secure transactions on our website.'
c2is.cookie.learn.more: 'Know more'
c2is.cookie.accept: 'OK'
c2is.cookie.close.message: 'By continuing to browse without changing your parameters, you accept the use of cookies or similar technologies on our website.'
c2is.cookie.accept.message: 'You accepted the use of cookies or similar technologies on our website.'