spatie / laravel-cookie-consent
Make your Laravel app comply with the crazy EU cookie law
Fund package maintenance!
spatie.be/open-source/support-us
Requires
- php: ^8.2
- illuminate/cookie: ^10.0|^11.0
- illuminate/support: ^10.0|^11.0
- illuminate/view: ^10.0|^11.0
- spatie/laravel-package-tools: ^1.9
Requires (Dev)
- fakerphp/faker: ^1.9
- orchestra/testbench: ^8.0|^9.0
- pestphp/pest: ^2.34
- dev-main
- 3.3.2
- 3.3.1
- 3.3.0
- 3.2.4
- 3.2.3
- 3.2.2
- 3.2.1
- 3.2.0
- 3.1.2
- 3.1.1
- 3.1.0
- 3.0.1
- 3.0.0
- v2.x-dev
- 2.12.13
- 2.12.12
- 2.12.11
- 2.12.10
- 2.12.9
- 2.12.8
- 2.12.7
- 2.12.6
- 2.12.5
- 2.12.4
- 2.12.3
- 2.12.2
- 2.12.1
- 2.12.0
- 2.11.0
- 2.10.0
- 2.9.0
- 2.8.0
- 2.7.0
- 2.6.3
- 2.6.2
- 2.6.1
- 2.6.0
- 2.5.0
- 2.4.1
- 2.4.0
- 2.3.0
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.0
- 2.0.0
- v1.x-dev
- 1.8.0
- 1.7.0
- 1.6.1
- 1.6.0
- 1.5.0
- 1.4.1
- 1.4.0
- 1.3.0
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.0
- 1.0.1
- 1.0.0
- 0.0.1
- dev-update-readme
This package is auto-updated.
Last update: 2024-12-20 14:07:31 UTC
README
This package adds a simple, customizable cookie consent message to your site. When the site loads, the banner appears and lets users consent to cookies. Once consent is given, the banner hides and stays hidden.
What this package does not:
- Include an option to 'Decline' all cookies, which might be required.
- Block trackers and cookies before consent. You need to handle this yourself.
- Include options for different consent categories like "Necessary" or "Marketing".
For more advanced cookie consent options in Laravel, consider these alternatives.
Support us
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
Installation
You can install the package via composer:
composer require spatie/laravel-cookie-consent
The package will automatically register itself.
Optionally you can publish the config-file:
php artisan vendor:publish --provider="Spatie\CookieConsent\CookieConsentServiceProvider" --tag="cookie-consent-config"
This is the contents of the published config-file:
return [ /* * Use this setting to enable the cookie consent dialog. */ 'enabled' => env('COOKIE_CONSENT_ENABLED', true), /* * The name of the cookie in which we store if the user * has agreed to accept the conditions. */ 'cookie_name' => 'laravel_cookie_consent', /* * Set the cookie duration in days. Default is 365 * 20. */ 'cookie_lifetime' => 365 * 20, ];
The cookie domain is set by the 'domain' key in config/session.php, make sure you add a value in your .env for SESSION_DOMAIN. If you are using a domain with a port in the url such as 'localhost:3000', this package will not work until you do so.
Usage
To display the dialog all you have to do is include this view in your template:
//in your blade template @include('cookie-consent::index')
This will render the following dialog that, when styled, will look very much like this one.
The default styling provided by this package uses TailwindCSS v2 to provide a floating banner at the bottom of the page.
When the user clicks "Allow cookies" a laravel_cookie_consent
cookie will be set and the dialog will be removed from the DOM. On the next request, Laravel will notice that the laravel_cookie_consent
has been set and will not display the dialog again
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="Spatie\CookieConsent\CookieConsentServiceProvider" --tag="cookie-consent-translations"
This will publish this file to resources/lang/vendor/cookie-consent/en/texts.php
.
return [ 'message' => 'Please be informed that this site uses cookies.', 'agree' => 'Allow cookies', ];
If you want to translate the values to, for example, French, just copy that file over to resources/lang/vendor/cookie-consent/fr/texts.php
and fill in the French 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="Spatie\CookieConsent\CookieConsentServiceProvider" --tag="cookie-consent-views"
This will copy the index
and dialogContents
view files over to resources/views/vendor/cookie-consent
. You probably only want to modify the dialogContents
view. If you need to modify the JavaScript code of this package you can do so in the index
view file.
Using the middleware
Instead of including cookie-consent::index
in your view you could opt to add the Spatie\CookieConsent\CookieConsentMiddleware
to your kernel:
// app/Http/Kernel.php class Kernel extends HttpKernel { protected $middleware = [ // ... \Spatie\CookieConsent\CookieConsentMiddleware::class, ]; // ... }
This will automatically add cookie-consent::index
to the content of your response right before the closing body tag.
Notice
We are not lawyers and can't provide legal advice. Consult legal professionals what rules apply to your project.
Changelog
Please see CHANGELOG for more information what has changed recently.
Testing
composer test
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security-related issues, please email freek@spatie.be instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.