The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

Laravel Cookie Consent

statikbe/laravel-cookie-consent image

Laravel Cookie Consent stats

Downloads
136.1K
Stars
112
Open Issues
8
Forks
21

View on GitHub →

Cookie consent modal for EU

Card of Laravel Cookie consent

Laravel cookie consent modal

Modal for Cookie consent

Preferences for cookies

The package includes a script & styling for a cookie banner and a modal where the visitor can select his/her cookie preferences.

This package is mainly based on the one from spatie: https://github.com/spatie/laravel-cookie-consent

With the only exception that you can choose which cookies you enable. This only works when Google Tag Manager is correctly configured (some regex config based on the value set in the cookie).

Upgrading

You can find our upgrading guides here.

Installation

You can install the package via composer:

composer require statikbe/laravel-cookie-consent

The package will automatically register itself.

First of all you need to publish the javascript and css files:

php artisan vendor:publish --provider="Statikbe\CookieConsent\CookieConsentServiceProvider" --tag="public"

Include the css/cookie-consent.css into your base.blade.php or any other base template you use.

<link rel="stylesheet" type="text/css" href="{{asset("vendor/cookie-consent/css/cookie-consent.css")}}">

The javascript file is included in the cookie snippet and will be added at the end of your body.

Usage

Instead of including a snippet in your view, we will automatically add it. This is done using middleware using two methods:

  1. The first option: include it in your entire project using the kernel:
// app/Http/Kernel.php
 
class Kernel extends HttpKernel
{
protected $middleware = [
// ...
\Statikbe\CookieConsent\CookieConsentMiddleware::class,
];
 
// ...
}
  1. The second option: include it as a route middleware and add this to any route you want.
// app/Http/Kernel.php
 
class Kernel extends HttpKernel
{
// ...
 
protected $routeMiddleware = [
// ...
'cookie-consent' => \Statikbe\CookieConsent\CookieConsentMiddleware::class,
];
}
 
 
// routes/web.php
Route::group([
'middleware' => ['cookie-consent']
], function(){
// ...
});

This will add cookieConsent::index to the content of your response right before the closing body tag.

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="Statikbe\CookieConsent\CookieConsentServiceProvider" --tag="lang"

This will publish this file to resources/lang/vendor/cookieConsent/en/texts.php.

 
return [
'alert_title' => 'Deze website gebruikt cookies',
'setting_analytics' => 'Analytische cookies',
];

If you want to translate the values to, for example, English, just copy that file over to resources/lang/vendor/cookieConsent/fr/texts.php and fill in the English 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="Statikbe\CookieConsent\CookieConsentServiceProvider" --tag="views"

This will copy the index view file over to resources/views/vendor/cookieConsent.

The cookie-settings view file is just a snippet you need to place somewhere onto your page. Most preferably in the footer next to the url of your cookie policy.

<a href="javascript:void(0)" class="js-lcc-settings-toggle">@lang('cookie-consent::texts.alert_settings')</a>

This gives your visitor the opportunity to change the settings again.

Publishing

Config

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

This is the contents of the published config-file: This will read the policy urls from your env.

return [
'cookie_key' => '__cookie_consent',
'cookie_value_analytics' => '2',
'cookie_value_marketing' => '3',
'cookie_value_both' => 'true',
'cookie_value_none' => 'false',
'cookie_expiration_days' => '365',
'gtm_event' => 'pageview',
'ignored_paths' => [],
'policy_url_en' => env('COOKIE_POLICY_URL_EN', null),
'policy_url_fr' => env('COOKIE_POLICY_URL_FR', null),
'policy_url_nl' => env('COOKIE_POLICY_URL_NL', null),
];

You can customize some settings that work with your GTM.

Don't show modal on cookie policy page or other pages

If you don't want the modal to be shown on certain pages you can add the relative url to the ignored paths setting. This also accepts wildcards (see the Laravel Str::is() helper).

'ignored_paths => ['/en/cookie-policy', '/api/documentation*'];

Translations

php artisan vendor:publish --provider="Statikbe\CookieConsent\CookieConsentServiceProvider" --tag="lang"

Views

php artisan vendor:publish --provider="Statikbe\CookieConsent\CookieConsentServiceProvider" --tag="views"

Configure Google Tag Manager

All the steps to configure your Google Tag Manager can be found here.

Security

If you discover any security related issues, please email info@statik.be instead of using the issue tracker.

License

The MIT License (MIT). Please see License File for more information.

statikbe photo

Building the web

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Statikbe Laravel Cookie Consent Related Articles

Laracon Online 2017 Recap image

Laracon Online 2017 Recap

Read article
How to Create A Most Popular List with Laravel and Google Analytics image

How to Create A Most Popular List with Laravel and Google Analytics

Read article
Laravel Analytics v2 image

Laravel Analytics v2

Read article
DreamzTech logo

DreamzTech

Hire 6-10+ Yrs. experienced skilled Laravel Developers from DreamzTech. We ensure NDA protected, 100% quality delivery. Contact Us & Discuss Your Need.

DreamzTech
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
LoadForge logo

LoadForge

Scalable load testing for web apps & APIs. Simulate real-world traffic and identify breaking points and performance limits with powerful, scalable load tests designed for Laravel.

LoadForge
The Certification of Competence for Laravel logo

The Certification of Competence for Laravel

A community-driven, proctored assessment across 4 levels designed to validate real-world Laravel knowledge, from Junior to mastery-level Artisan. Official Vue.js, Official Nuxt, Angular, React, JS certifications also available.

The Certification of Competence for Laravel
Tighten logo

Tighten

We help companies turn great ideas into amazing apps, products, and services.

Tighten
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell