digifactory/laravel-cookie-consent

轻松处理Laravel应用程序中的cookie同意

v4.0.0 2024-04-16 08:10 UTC

This package is auto-updated.

Last update: 2024-09-16 08:59:21 UTC


README

Latest Version on Packagist MIT Licensed GitHub Workflow Status Quality Score StyleCI Total Downloads

此包使您在Laravel应用程序和Blade视图中处理cookie同意变得简单。默认情况下,该包使用Cookiebot作为其“同意提供者”。它不会替换Cookiebot(或任何其他同意提供者)的JavaScript实现。需要Laravel v6.5,这是为了unless Blade指令。

安装

您可以通过composer安装此包

composer require digifactory/laravel-cookie-consent

您可以发布配置文件

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

用法

默认情况下启用cookie同意。这意味着对于所有条件,我们使用同意提供者来检查用户是否已给出同意。您可以通过创建一个环境变量COOKIE_CONSENT_ENABLED并将其设置为false来禁用cookie同意。如果禁用cookie同意,则所有检查都将返回true,因此所有cookie都被允许,就像用户已同意所有类型的cookie一样。

Blade

您可以在视图中使用以下Blade指令

  • cookieConsentNecessary
  • unlesscookieConsentNecessary
  • elsecookieConsentNecessary
  • endcookieConsentNecessary
  • cookieConsentPreferences
  • unlesscookieConsentPreferences
  • elsecookieConsentPreferences
  • endcookieConsentPreferences
  • cookieConsentStatistics
  • unlesscookieConsentStatistics
  • elsecookieConsentStatistics
  • endcookieConsentStatistics
  • cookieConsentMarketing
  • unlesscookieConsentMarketing
  • elsecookieConsentMarketing
  • endcookieConsentMarketing

您可以这样做

@cookieConsentStatistics
<script>
    <!-- Put your analytics code here! -->
</script>
@endcookieConsentStatistics

@cookieConsentMarketing
    <iframe width="560" height="315" src="https://www.youtube.com/embed/fzQSE_3eLKk" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
@else
    Please allow marketing cookies to view this video. Click <a href="javascript: Cookiebot.renew()">here</a> to renew or change your cookie consent.
@endcookieConsentMarketing

@unlesscookieConsentPreferences
    We cannot save your preferences because you did not allow preference cookies.
@endcookieConsentPreferences

PHP

或者检查PHP代码中的给定同意

CookieConsent::forNecessary();
CookieConsent::forPreferences();
CookieConsent::forStatistics();
CookieConsent::forMarketing();

如果您不想使用外观,则可以使用app('cookie-consent')

app('cookie-consent')->forNecessary();
app('cookie-consent')->forPreferences();
app('cookie-consent')->forStatistics();
app('cookie-consent')->forMarketing();

实现自己的同意提供者

您的同意提供者应实现ConsentProvider合同

<?php

namespace DigiFactory\CookieConsent\Contracts;

interface ConsentProvider
{
    public function forNecessary(): bool;

    public function forPreferences(): bool;

    public function forStatistics(): bool;

    public function forMarketing(): bool;
}

您可以在配置中覆盖默认提供者

<?php

return [
    /**
     * By default this package uses Cookiebot to determine if certain
     * types of cookies are allowed. Of course you can use a provider
     * of your own, your provider should implement the ConsentProvider
     * interface.
     */
    'provider' => \DigiFactory\CookieConsent\Providers\Cookiebot::class,
];

测试

composer test

更改日志

有关最近更改的更多信息,请参阅更改日志

贡献

有关详细信息,请参阅贡献

安全

如果您发现任何安全相关的问题,请通过电子邮件mark@digifactory.nl联系,而不是使用问题跟踪器。

鸣谢

许可

MIT许可(MIT)。有关更多信息,请参阅许可文件

Laravel包模板

此包是使用Laravel包模板生成的。