This package makes dealing with cookie consent in your Laravel app and Blade views a piece of cake. By default the package uses Cookiebot as its 'consent provider'. It doesn't replace the Cookiebot's (or any other consent provider's) JavaScript implementation.
Laravel v6.5 is required, this is necessary for the unless
Blade directive.
You can install the package via composer:
composer require digifactory/laravel-cookie-consent
You can publish the config file:
php artisan vendor:publish --provider="DigiFactory\CookieConsent\CookieConsentServiceProvider" --tag="config"
By default cookie consent is enabled. This means for all conditionals we use the consent provider to check if the user has given consent. You can disable cookie consent by creating an environment variable COOKIE_CONSENT_ENABLED
and set it to false
. If cookie consent is disabled all checks will return true
, so all cookies are allowed as if the user has given consent for all types of cookies.
You can use the following Blade directives in your views:
cookieConsentNecessary
unlesscookieConsentNecessary
elsecookieConsentNecessary
endcookieConsentNecessary
cookieConsentPreferences
unlesscookieConsentPreferences
elsecookieConsentPreferences
endcookieConsentPreferences
cookieConsentStatistics
unlesscookieConsentStatistics
elsecookieConsentStatistics
endcookieConsentStatistics
cookieConsentMarketing
unlesscookieConsentMarketing
elsecookieConsentMarketing
endcookieConsentMarketing
You can do something like this:
@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
Or check for given consent in your PHP code:
CookieConsent::forNecessary();
CookieConsent::forPreferences();
CookieConsent::forStatistics();
CookieConsent::forMarketing();
If you don't want to use the Facade then you can use app('cookie-consent')
:
app('cookie-consent')->forNecessary();
app('cookie-consent')->forPreferences();
app('cookie-consent')->forStatistics();
app('cookie-consent')->forMarketing();
You consent provider should implement the ConsentProvider
contract:
<?php
namespace DigiFactory\CookieConsent\Contracts;
interface ConsentProvider
{
public function forNecessary(): bool;
public function forPreferences(): bool;
public function forStatistics(): bool;
public function forMarketing(): bool;
}
You can override the default provider in the config:
<?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
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.
This package was generated using the Laravel Package Boilerplate.