Laravel Package

Add a cookie consent to your Laravel application in just 5 minutes

Max Hutschenreiter -

In this short tutorial, we will show you how you can add a GDPR compliant cookie popup to your Laravel site.

cookie popup


Find the Package on the Github!

Lately, the Courts decided that you need to give your users the option to opt-in for every third-party service you are using. A lot of our clients were asking about a popup like this. Unfortunately, the famous Spatie package we used on most of our Sites is not addressing this. That was the point where we decided to write our own EU Cookie Consent package. 
 
Our Goals have been to give users the option to opt-in per service and the developers to check if the user gave permission. As a small goodie, we implemented also an easy way to play out the allowed scripts to your page.

Installation:
You can install the package via composer:
composer require the42coders/eu-cookie-consent

Customize the popup:
Most of the customization can be done through the config file. First, you need to publish it.
php artisan vendor:publish --provider="the42Coders\eu-cookie-consent\EuCookieConsentServiceProvider" --tag="config" 
The config file has a lot of comments to help you get started. 

The core of the popup is defined in the cookies array. Here you can define the categories and inside of the categories the cookies. Categories are meant to organize the cookie permissions to topics. But feel free to just use one if it fits you.
Every parameter in the array which is marked with Optional: in the comment above is optional :). Let's assume you want to add a Facebook cookie permission to the popup.

This is how the cookies part is looking at the moment.
cookies' => [
    //The key defines the key in the translations and is used to access the Cookie specific information
    'session' => [
        //Optional: you can set forced to make it impossible for the user to not accept this cookie.
        'forced' => 'true',
        //Optional: The description defines the key in the translations
        //'description' => 'key in translation File'
    ],
    'xsrf-token' => [
        'forced' => 'true',
    ],
],
Now we add facebook permission to it.
cookies' => [
    //The key defines the key in the translations and is used to access the Cookie specific information
    'session' => [
        //Optional: you can set forced to make it impossible for the user to not accept this cookie.
        'forced' => 'true',
        //Optional: The description defines the key in the translations
        //'description' => 'key in translation File'
    ],
    'xsrf-token' => [
        'forced' => 'true',
    ],
    'facebook' => [],
],
Since this Package has multi-language support all the texts are set in the language files. To make changes we need to publish them first.
php artisan vendor:publish --provider="the42Coders\eu-cookie-consent\EuCookieConsentServiceProvider" --tag="lang" 
In the language file, you just need to add a new key and the content.
'facebook' => 'We want facebook to know everything about you. Please let us :).',
That's it! You added new cookie permission to the popup. You can also delete the already existing ones if you don't need them.

Use the Popup:
Now you want to show the popup on your Laravel site. We recommend to integrate it into your base blade file like the app.blade.php. 
This will render the Popup HTML and that is all you need.
{!! EuCookieConsent::getPopup() !!}

Check for Permissions:
But now you want to integrate the Facebook connection based on the permission of the user. The package gives you two ways to do so.
1. Check for permission in your blade file.
<head>
    @if(EuCookieConsent::canIUse('facebook'))
        <script>
            alert('all the crazy facebook script stuff');
        </script>
    @endif
</head>
This will insert the Script tag only if the User agreed to it in the popup.

2. Mass render them in your blade file.
To do this we need to make a small adjustment to the config file.
'facebook' => [
    'header' => '<script>alert(\'all the crazy facebook script stuff\');</script>',
],
You can define any non taken key here as you will see later. You just need to use the same on all the cookie permissions you want to render at the same place.
In your blade File, you can do this.
<head>
    {!! EuCookieConsent::getHtml('header') !!}
</head>
This will collect the header tag from all the cookie permissions the user allowed you to use and just output them here. If you want to have one part in the header and one in the footer you can just define both in the config array.

You made it!
Your page is now cookie GDPR compliant. 
If you have any ideas for improvements to this package feel free to contact us.
Tags: Laravel · open-source · Laravel package · cookies · gdpr · package

Want products news and updates?

Sign up for our newsletter to stay up to date.

We care about the protection of your data. Read our Privacy Policy.

Impressions from our Team

  • Happy birthday πŸŽπŸŽˆπŸŽ‚ Filip - #

  • Another day another #mandarinacakeshop πŸŽ‚ πŸ˜€ - #

  • Happy Birthday Ognjen! And marry Christmas to all other πŸŽ„#notacakeshop - #

  • #Office #Garden - #

  • #workhard - #

  • #belgrade #skyline - #

  • #happybirthday Phil :) - #

  • #happybirthday Stefan πŸ₯‚ - #

  • #happybirthday Lidija 🍾 - #

  • Say hi πŸ‘‹ to our newest team member β˜•οΈ - #

  • #bithday #cake 😻 - #

  • #stayathome #homeoffice #42coders - #

  • #stayathome #homeoffice #42coders #starwars :) - #

  • #stayathome #homeoffice #42coders - #

  • We had a really nice time with #laracononline #laravel - #

  • Happy Birthday πŸŽ‚ MiloΕ‘ - #

  • Happy Birthday πŸŽ‚Nikola - #

  • #42coders #christmas #dinner what a nice evening :) - #

  • Happy Birthday πŸŽ‚ Ognjen - #

  • Wish you all a merry Christmas πŸŽ„πŸŽ - #

See more!

© 2024 42coders All rights reserved.