c2is/cookie-bundle 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

c2is/cookie-bundle

Composer 安装命令:

composer require c2is/cookie-bundle

包简介

Includes twig and css templating, js and php logic allowing the display of a cookie acceptance message

README 文档

README

Bundles the resources and logic needed to display a cookie acceptance message on a website. The message disapears after the user closed it three times or explicitely clicked on "accept".

Includes a default twig + css template.

Install

With composer:

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

You will have to register the bundle into your AppKernel. The bundle depends on FOSJsRoutingBundle for the default javascript behaviour, so if you don't have it installed already, register that aswell:

// 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(),
        );
    }
}

Usage

You need to add the bundle routing configuration to your application routing like so:

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

There are three routes used in this bundle. One serves the HTML template for your cookie acceptance panel, the others are called with Ajax requests to register the user actions.

To display the message, I recommend using an esi - the cache duration is managed by the plugin. This will allow the message to appear to a new user whatever your page cache configuration if you use HTTPCacheing. If you don't, it will fallback to the render Twig function and work aswell, so no worries. The default tempalte works best if placed just before or close to the body closing tag in your template.

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

The default template comes with a default css you can include aswell:

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

For the whole thing to work properly you will need to include the javascript (you will need jquery aswell):

<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="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
        <script src="{{ asset('bundles/c2iscookie/js/jquery.cookie.min.js') }}"></script>
    </body>
</html>

All this will be for naught before you install your assets though:

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

Configuration

Bundle

The exhaustive default configuration is as follows:

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

The default twig template creates a div with an id of "cookiesLegalMessage". The JQuery plugin used by the bundle expects a container with that id to bind itself to.

If you override the Twig template and create your own panel, you can initialize the JQuery plugin like so:

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

Available configurations for that plugin is:

  • on_closed:

    defaults to false Can be a function that will be executed when the user closes the cookie acceptance panel. Will receive as argument a json array with the values success: true and message: 'a confirmation message'

  • on_accepted:

    defaults to false Can be a function that will be executed when the user accepts the cookie. Will receive as argument a json array with the values success: true and message: 'a confirmation message'

There also are events fired when the user closes or accepts the cookie panel:

  • cookie_closed
  • cookie_accepted

The default plugin behaviour on those actions is to hide the cookie panel. This is done before triggering the events, so you can display it back again in your event listener if you want to display a confirmation message or something. The events are fired from the container so you'll want to listen to that:

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

Overriding

You can override parts of this bundle to better fit your application needs

Template

You can create a twig template of your own:

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

Messages and translations

The exhaustive list of messages used and their default english value:

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.'

c2is/cookie-bundle 适用场景与选型建议

c2is/cookie-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14.61k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2015 年 03 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 c2is/cookie-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 c2is/cookie-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 c2is/cookie-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 14.61k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 17
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 4
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-03-18