定制 statikbe/laravel-cookie-consent 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

statikbe/laravel-cookie-consent

Composer 安装命令:

composer require statikbe/laravel-cookie-consent

包简介

Cookie consent modal for EU

README 文档

README

Card of Laravel Cookie consent

Laravel cookie consent modal

Latest Version on Packagist Total Downloads

Modal cookie consent

Preferences Modal

The package includes a script & styling for a cookie banner and a modal where the visitor can select his/her cookie preferences.

This package is mainly based on the one from spatie: https://github.com/spatie/laravel-cookie-consent

With the only exception that you can choose which cookies you enable. This only works when Google Tag Manager is correctly configured (some regex config based on the value set in the cookie).

Upgrading

You can find our upgrading guides here.

Installation

You can install the package via composer:

composer require statikbe/laravel-cookie-consent

The package will automatically register itself.

First of all you need to publish the javascript and css files:

php artisan vendor:publish --provider="Statikbe\CookieConsent\CookieConsentServiceProvider" --tag="cookie-public"

When using the default theme, make sure to include the css/cookie-consent.css into your base.blade.php or any other base template you use.

<link rel="stylesheet" type="text/css" href="{{asset("vendor/cookie-consent/css/cookie-consent.css")}}">

If you want to use the filament theme, make sure to source the blade files in your tailwind config

tailwind v4:

/* point to the actual vendor location */
@source 'vendor/statikbe/laravel-cookie-consent/resources/**/*.blade.php';

tailwind v3 and below:

export default {
    content: [
        'vendor/statikbe/laravel-cookie-consent/resources/**/*.blade.php';
    ]
}

The javascript file is included in the cookie snippet and will be added at the end of your body.

If you want to show the filament themed cookie banner outside of your filament panel, also make sure that you include the filament styles and scripts in your templates.

Usage

Instead of including a snippet in your view, we will automatically add it. This is done using middleware using two methods:

For Laravel 11.x and newer

// bootstrap/app.php

->withMiddleware(function (Middleware $middleware) {
    ...
    $middleware->web(append: [
        ...
        \Statikbe\CookieConsent\CookieConsentMiddleware::class,
    ]);

// OR AS AN ALIAS

    $middleware->alias([
        ...
        'cookie-consent' => \Statikbe\CookieConsent\CookieConsentMiddleware::class,
    ]);
})

For Laravel 10.x and earlier

// app/Http/Kernel.php

class Kernel extends HttpKernel
{
    protected $middleware = [
        // ...
        \Statikbe\CookieConsent\CookieConsentMiddleware::class,
    ];

     protected $routeMiddleware = [
        // ...
        'cookie-consent' => \Statikbe\CookieConsent\CookieConsentMiddleware::class,
    ];


// routes/web.php
Route::group([
    'middleware' => ['cookie-consent']
], function(){
    // ...
});
}

This will add cookieConsent::index to the content of your response right before the closing body tag.

Customising the dialog texts

If you want to modify the text shown in the dialog you can publish the lang-files with this command:

php artisan vendor:publish --provider="Statikbe\CookieConsent\CookieConsentServiceProvider" --tag="cookie-lang"

This will publish this file to resources/lang/vendor/cookieConsent/en/texts.php.

return [
    'alert_title' => 'Deze website gebruikt cookies',
    'setting_analytics' => 'Analytische cookies',
];

If you want to translate the values to, for example, English, just copy that file over to resources/lang/vendor/cookieConsent/fr/texts.php and fill in the English translations.

Customising the dialog contents

If you need full control over the contents of the dialog. You can publish the views of the package:

php artisan vendor:publish --provider="Statikbe\CookieConsent\CookieConsentServiceProvider" --tag="cookie-views"

This will copy the index view file over to resources/views/vendor/cookieConsent.

The cookie-settings view file is just a snippet you need to place somewhere onto your page. Most preferably in the footer next to the url of your cookie policy.

<a href="javascript:void(0)" class="js-lcc-settings-toggle">@lang('cookie-consent::texts.alert_settings')</a>

This gives your visitor the opportunity to change the settings again.

Customising the theme

By default, the cookie popup will look like this:

screenshot of default theme

If you are however working on a project that is using filament, you can opt to use filament componets to render your cookie popup.

To do this, configure the theme to filament

'theme' => 'filament'

This will render the cookie popup like so:

screenshot of filament theme

Publishing

Config

php artisan vendor:publish --provider="Statikbe\CookieConsent\CookieConsentServiceProvider" --tag="cookie-config"

This is the contents of the published config-file: This will read the policy urls from your env.

return [
    /**
     * Theme to use for the cookie consent popup.
     * Available options: 'default', 'filament'.
     */
    'theme' => 'default',
    /**
     * Filament render hook where the package will
     * register a navigation item to show the cookie
     * settings modal. 
     * Set to null to disable this.
     */
    'filament-nav-item-render-hook' => PanelsRenderHook::USER_MENU_PROFILE_AFTER,
    'disable_filament_nav_hook' => false,
    'cookie_key' => '__cookie_consent',
    'cookie_value_analytics' => '2',
    'cookie_value_marketing' => '3',
    'cookie_value_both' => 'true',
    'cookie_value_none' => 'false',
    'cookie_expiration_days' => '365',
    'gtm_event' => 'pageview',
    'ignored_paths' => [],
    /**
     * Skip cookie consent on error responses (4xx and 5xx status codes).
     * Set to true if you want to disable cookie banner on error pages.
     */
    'skip_on_error_responses' => false,
    'cookie_secure' => env('COOKIE_CONSENT_SECURE', false),
    'policy_url_en' => env('COOKIE_POLICY_URL_EN', null),
    'policy_url_fr' => env('COOKIE_POLICY_URL_FR', null),
    'policy_url_nl' => env('COOKIE_POLICY_URL_NL', null),
];

You can customize some settings that work with your GTM.

Don't show modal on cookie policy page or other pages

If you don't want the modal to be shown on certain pages you can add the relative url to the ignored paths setting. This also accepts wildcards (see the Laravel Str::is() helper).

'ignored_paths => ['/en/cookie-policy', '/api/documentation*'];

Skip cookie consent on error pages

By default, the cookie consent banner is shown on error pages (404, 500, etc.). If you want to disable the banner on error responses, you can set this option to true:

'skip_on_error_responses' => true,

Translations

php artisan vendor:publish --provider="Statikbe\CookieConsent\CookieConsentServiceProvider" --tag="cookie-lang"

Views

php artisan vendor:publish --provider="Statikbe\CookieConsent\CookieConsentServiceProvider" --tag="cookie-views"

Configure Google Tag Manager

All the steps to configure your Google Tag Manager can be found here.

Security

If you discover any security related issues, please email info@statik.be instead of using the issue tracker.

License

The MIT License (MIT). Please see License File for more information.

statikbe/laravel-cookie-consent 适用场景与选型建议

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

它主要适用于以下技术方向: 「laravel」 「cookie」 「privacy」 「Google Tag Manager」 「consent」 「gdpr」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 219
  • Watchers: 8
  • Forks: 27
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-02-03