Adding cookie consent to your Laravel site in under 5 minutes

Adding cookie consent to your Laravel site in under 5 minutes

Implement a cookie consent package and stay GDPR compliant in a few easy steps

Implement a cookie consent package and stay GDPR compliant in a few easy steps
Add cookie consent to your Laravel project

Need a new t-shirt?


You will no doubt be very aware of the cookie consent banners that started appearing on websites a few years ago as a result of EU legislation, and which seem to have become even more prevalent since the arrival of GDPR in Europe.

We’ve had a few customers request the addition of these banners to their sites, and depending on the technology behind the website and the customisation requirements this can be a very quick task or it can take a reasonable amount of time to implement properly.

Luckily for Laravel websites, the good people at Spatie have made the laravel-cookie-consent package that can be installed, customised and implemented in under 5 minutes, so let’s get started!

Get our help adding cookie consent to your site

Installation:

The package is installed with composer, just use:

composer require spatie/laravel-cookie-consent

That’s it for the installation part, easy huh? 🚀

Customisation:

To publish the configuration file, (optional) use:

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

You can enable or disable the cookie consent bar, change the name of the cookie and set its life from these settings, but the defaults are likely to be fine for most people, so you can probably skip this step.

To publish the language files to change the message and button text, use:

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

You’ll find the language files in: resources/lang/vendor/cookieConsent/

The default message is “Your experience on this site will be improved by allowing cookies.” and the button text is: “Allow cookies”, so again if you’re happy with this then feel free to skip this step too.

Get our help adding cookie consent to your site

To publish the views used for the cookie consent bar itself, use:

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

You’ll find the view files in: /resources/views/vendor/cookieConsent

You can add extra classes if required to any of the elements here, for example if you’re using Bootstrap 4, you can add btn btn-sm btn-primary to the <button> classes and fixed-bottom bg-success p-2 text-light text-center to the main <div class="js-cookie-consent cookie-consent"> .

This will create the bar like you see on welcm.uk — fixed to the bottom of the screen with a small blue button and centered white text.

If you need hosting for your Laravel projects, we like DigitalOcean, you can get $100 hosting credit to try it out if you sign up via this link.

CSS

You can customise the cookie consent banner further by adding your own styling to the existing classes, or add new ones if required. Just make sure to not delete or rename the existing classes or the javascript that sets the cookie and hides the bar will not fire when users click the accept button.

Implementation:

In your blade template, simply add:

@include('cookieConsent::index')

Typically you would add this in your layout file so it appears on all pages, but you can choose to display it only on specific pages or use @if blade directives to set your own logic for when the bar is displayed.

Conclusion:

That’s it! You should now have a fully functioning and customised cookie consent banner on your Laravel site in under 5 minutes!

We’re happy to help with all aspects of Laravel development so if we can help with this or anything else for your website or app, feel free to get in touch!

Get our help adding cookie consent to your site
Related posts:
Laravel Wink - A Beginner's guide
Adding Google ReCaptcha to forms in Laravel
Multiple websites from one Laravel instance