code16 / cookie-consent
Cookie consent bar & manage modal
Installs: 6 540
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 1
pkg:composer/code16/cookie-consent
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: 2025-10-15 17:38:12 UTC
README
Setup
composer require code16/cookie-consent
Required: publish assets (add this in composer.json post-autoload-dump scripts)
php artisan vendor:publish --provider='Code16\CookieConsent\CookieConsentServiceProvider' --tag=assets --force
You may publish the config file:
php artisan vendor:publish --provider="Code16\CookieConsent\CookieConsentServiceProvider" --tag=config
And the lang file:
php artisan vendor:publish --provider="Code16\CookieConsent\CookieConsentServiceProvider" --tag=lang
Usage
Default
In your blade layout
<head> {{-- ... --}} @cookies <script> {{-- some injected cookies --}} </script> @endcookies </head> <body> {{-- end of the body --}} @include('cookieConsent::index') </body>
With category
To let the user manage multiple cookie categories (e.g. analytics, socials)
Add the category key to the @cookies directive
<head> {{-- ... --}} @cookies('analytics') <script> {{-- some analytics script --}} </script> @endcookies </head>
Also you must declare the cookie category in config/cookie-consent.php as follow
[
'cookie_categories' => [
'system' => [
'required' => true,
],
'analytics' => [],
]
];
Categories marked as required are cannot be opt-out by the user.
To provide explanation texts in the manage dialog, add content to the lang file:
[
'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",
],
],
]
];
Show the manage modal from a link (e.g. cookies page)
In the page:
@section('content') <a href="#manage-cookies">Open manage cookies modal</a> @endsection
Intercept accept POST request
If you need to add some custom logic when cookies are accepted (meaning: either when the used clicked on OK in the Bar or after setting his choices on the Modal), you can define a Middleware in the cookie-consent-middleware config key, which will be executed on the POST request.
Example:
// in config/cookie-consent.php return [ // ... 'middleware' => 'cookie-consent.accepted' ];