code16 / cookie-consent
该软件包最新版本(v2.0.0-alpha.3)没有可用的许可信息。
Cookie 同意栏 & 管理窗口
v2.0.0-alpha.3
2021-08-12 17:17 UTC
Requires
- php: >=7.3
- illuminate/cookie: ~5.8.0|^6.0|^7.0|^8.0
- illuminate/support: ~5.8.0|^6.0|^7.0|^8.0
- illuminate/view: ~5.8.0|^6.0|^7.0|^8.0
Requires (Dev)
- fzaninotto/faker: ~1.4
- mockery/mockery: 1.3.*
- orchestra/testbench: 4.*|5.*|6.*
- phpunit/phpunit: ~8.0|^8.5|~9.0
This package is auto-updated.
Last update: 2024-09-15 14:55:47 UTC
README
设置
composer require code16/cookie-consent
必需:发布资产(在 composer.json 中添加到 post-autoload-dump
脚本中)
php artisan vendor:publish --provider='Code16\CookieConsent\CookieConsentServiceProvider' --tag=assets --force
您可能需要发布配置文件
php artisan vendor:publish --provider="Code16\CookieConsent\CookieConsentServiceProvider" --tag=config
以及语言文件
php artisan vendor:publish --provider="Code16\CookieConsent\CookieConsentServiceProvider" --tag=lang
用法
默认
在您的 blade 布局中
<head> {{-- ... --}} @cookies <script> {{-- some injected cookies --}} </script> @endcookies </head> <body> {{-- end of the body --}} @include('cookieConsent::index') </body>
带有分类
为了使用户能够管理多个 Cookie 分类(例如分析、社交等),将分类键添加到 @cookies
指令中
<head> {{-- ... --}} @cookies('analytics') <script> {{-- some analytics script --}} </script> @endcookies </head>
此外,您必须在 config/cookie-consent.php
中声明 Cookie 分类,如下所示
[ 'cookie_categories' => [ 'system' => [ 'required' => true, ], 'analytics' => [], ] ];
标记为 required
的分类用户不能选择退出。
为了在管理对话框中提供说明性文本,请向语言文件添加内容
[ 'manage' => [ 'title' => 'Manage cookies', 'description' => 'About cookies...', 'categories' => [ 'system' => [ 'title' => 'System cookies', 'description' => "Description text about system cookies", ], 'analytics' => [ 'title' => 'Analytics cookies', 'description' => "Description text about analytics cookies", ], ], ] ];
通过链接显示管理窗口(例如 cookies 页面)
在页面中
@section('content') <a href="#manage-cookies">Open manage cookies modal</a> @endsection
拦截接受 POST 请求
如果您需要在接受 Cookie 时添加一些自定义逻辑(即:当用户在栏中点击 OK 或者在模态窗口中设置其选择后),您可以在 cookie-consent-middleware 配置键中定义一个中间件,该中间件将在 POST 请求上执行。
示例
// in config/cookie-consent.php return [ // ... 'middleware' => 'cookie-consent.accepted' ];