Make your Laravel app comply with the crazy EU cookie law

Overview

Make your Laravel app comply with the crazy EU cookie law

Latest Version on Packagist Software License GitHub Workflow Status Total Downloads

All sites owned by EU citizens or targeted towards EU citizens must comply with a crazy EU law. This law requires a dialog to be displayed to inform the users of your websites how cookies are being used. You can read more info on the legislation on the site of the European Commission.

This package provides an easily configurable view to display the message. Also included is JavaScript code to set a cookie when a user agrees with the cookie policy. The package will not display the dialog when that cookie has been set.

Spatie is a web design agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

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="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,
];

Usage

To display the dialog all you have to do is include this view in your template:

//in your blade template
@include('cookieConsent::index')

This will render the following dialog that, when styled, will look very much like this one.

dialog

Please be aware that the package does not provide any styling, this is something you'll need to do yourself.

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="lang"

This will publish this file to resources/lang/vendor/cookieConsent/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/cookieConsent/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="views"

This will copy the index and dialogContents view files over to resources/views/vendor/cookieConsent. 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 cookieConsent::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 cookieConsent::index to the content of your response right before the closing body tag.

Notice

The legislation is pretty very vague on how to display the warning, which texts are necessary, and what options you need to provide. This package will go a long way towards compliance, but if you want to be 100% sure that your website is ok, you should consult a legal expert.

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 [email protected] instead of using the issue tracker.

Credits

License

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

Comments
  • Added refuse functionality

    Added refuse functionality

    I added the possibility to display a refuse button on the cookie consent dialog. I decided to keep it small. It's optional (disabled by default), and everything is tested and documented in the README.

    Translations are based on google translate.

    I also fixed a little thing in the isConsentDialogDisplayed method as the translation key seems to have changed from button_text to agree and it was not the case in the tests.

    opened by WhereIsLucas 4
  • Add PHP 8-only Support (v3)

    Add PHP 8-only Support (v3)

    This PR adds a new major version, v3.0.0.

    Specifically, it:

    • Removes support for all PHP 7.x versions.
    • Requires PHP 8.0+.
    • All syntax converted to PHP 8 where possible.
    • Removes unnecessary PHP docblocks per Spatie's guidelines.
    • Removes support for Laravel 6.x.
    • Implements spatie/laravel-package-tools.
    • Updates the changelog - release date needs to be updated, currently is "unreleased".
    opened by patinthehat 3
  • Use session.same_site to set the Cookie's SameSite Attribute

    Use session.same_site to set the Cookie's SameSite Attribute

    As more and more browsers start enforcing SameSite and Secure flags of a cookie, the default Laravel config prevents this package from setting the Secure flag of a cookie and some browsers, such as Firefox 78, will output a message like this one after the cookie has been set:

    Cookie “laravel_cookie_consent” will be soon rejected because it has the “sameSite” attribute set to “none” or an invalid value, without the “secure” attribute. To know more about the “sameSite“ attribute, read https://developer.mozilla.org/docs/Web/HTTP/Headers/Set-Cookie/SameSite

    Which will eventually lead to this package not working correctly unless session.secure has been set correctly. However, an easy fix is possible: session.same_site defines the SameSite policy, which defaults to lax.

    By inheriting these values for this package's cookie, it will not only be future-proof (for now) but also consistent behavior with other Laravel cookies is ensured.

    This PR adds the functionality described above and does not introduce any new config keys.

    Please let me know if you'd like me to make any changes. And a big thank you for all the many fantastic packages you've made available!

    opened by limenet 3
  • Replace trans helper

    Replace trans helper

    Replaced the trans helper function to use the recommended __() helper, as described in the docs:

    https://laravel.com/docs/master/localization#retrieving-translation-strings

    opened by Erulezz 2
  • Laravel 8.x Compatibility

    Laravel 8.x Compatibility

    This is an automated pull request from Shift to update your package code and dependencies to be compatible with Laravel 8.x.

    Before merging, you need to:

    • Checkout the l8-compatibility branch
    • Review all comments for additional changes
    • Thoroughly test your package

    If you do find an issue, please report it by commenting on this PR to help improve future automation.

    opened by laravel-shift 2
  • Add ability to inject the view content via middleware

    Add ability to inject the view content via middleware

    By adding CookieConsentMiddleware::class to the web middleware, the view will be injected by default before the closing body tag </body>.

    I also added a missing dependency which was causing the build to fail earlier.

    opened by themsaid 2
  • Add contrast on text, anchor -> button

    Add contrast on text, anchor -> button

    This is to improve the google lighthouse score by default.

    Text color was reported as not enough contrast by default which is an accessibility issue. Button is more appropriate than link and it reports it as non crawl-able.

    opened by blite 1
  • Use TailwindCSS styling by default

    Use TailwindCSS styling by default

    This PR changes the default dialog styling to use TailwindCSS.

    Specifically, it:

    • resolves issue/feature request #158.
    • provides styling for desktop/tablet browsers:

    image

    • ...and mobile devices:

    image

    • removes an unused test snapshot.
    • adds an additional test to check for existence of the css classes necessary for javascript functionality.
    • updates the readme - the screenshot in the readme may need to be updated.
    • updates the changelog - release date needs to be updated, currently is "unreleased".
    opened by patinthehat 1
Releases(3.2.2)
🍪 Laravel cookie consent history

?? Laravel cookie consent history This packages aims to make it easy to store anonymous cookie consents in a database in order to comply with rather s

Dystopia 3 Nov 9, 2022
🍪 Powerful wrapper for improved cookie integration on Laravel

?? Powerful wrapper for improved cookie integration on Laravel

Attla 1 Jul 16, 2022
Make requests to the Shopify API from your Laravel app

Make requests to the Shopify API from your Laravel app The signifly/laravel-shopify package allows you to easily make requests to the Shopify API. Ins

Signifly 147 Dec 30, 2022
Make any class queue aware in a Laravel app

Make any class queue aware This is where your description should go. Limit it to a paragraph or two. Consider adding a small example. Support us We in

Spatie 8 Jun 17, 2022
A simple blog app where a user can signup , login, like a post , delete a post , edit a post. The app is built using laravel , tailwind css and postgres

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

Nahom_zd 1 Mar 6, 2022
Laravel-OvalFi helps you Set up, test, and manage your OvalFi integration directly in your Laravel App.

OvalFi Laravel Package Laravel-OvalFi helps you Set up, test, and manage your OvalFi integration directly in your Laravel App. Installation You can in

Paul Adams 2 Sep 8, 2022
A package to easily make use of Iconic icons in your Laravel Blade views.

Blade Iconic A package to easily make use of Iconic icons in your Laravel Blade views. For a full list of available icons see the SVG directory. Iconi

Malik Alleyne-Jones 17 Aug 25, 2022
A package to easily make use of Simple Icons in your Laravel Blade views.

Blade Simple Icons A package to easily make use of Simple Icons in your Laravel Blade views. For a full list of available icons see the SVG directory.

UB Labs 12 Jan 17, 2022
A package to easily make use of SVG icons in your Laravel Blade views.

Blade Icons A package to easily make use of SVG icons in your Laravel Blade views. Originally "Blade SVG" by Adam Wathan. Turn... <!-- camera.svg -->

Blade UI Kit 1.7k Jan 2, 2023
Make your own custom cast type for Laravel model attributes

Laravel Custom Casts Make your own cast type for Laravel model attributes Laravel custom casts works similarly to Eloquent attribute casting, but with

Vladimir Ković 220 Oct 28, 2022
A package to easily make use of Iconsax in your Laravel Blade views.

Blade Iconsax A package to easily make use of Iconsax in your Laravel Blade views. This package contains 1.000 icons in 6 diferent styles, a total of

Guilherme Saade 4 Oct 22, 2022
A web app for detecting backend technologies used in a web app, Based on wappalyzer node module

About Techdetector This a web fingerprinting application, it detects back end technologies of a given domain by using the node module wappalyzer. And

Shobi 17 Dec 30, 2022
CV-Resumes-App is helped us to build resume .. you can help me to improve this app...

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

Eng Hasan Hajjar 2 Sep 30, 2022
Podcastwala - Your very own Podcast web app built with Laravel. Manage and listen to your favorite podcasts

Podcastwala Your very own Podcast web app built with Laravel 5. This web app enables you to manage RSS feeds for your favorite podcasts and listen to

null 142 Sep 14, 2022
A package that makes it easy to have the `artisan make:` commands open the newly created file in your editor of choice.

Open On Make A package that makes it easy to have the artisan make: commands open the newly created file in your editor of choice. Installation compos

Andrew Huggins 94 Nov 22, 2022
Make your accessors smarter

Computed properties for Eloquent Laravel 5.4+ Based on this tweet: https://twitter.com/reinink/status/899713609722449920 Some examples for better unde

Nikita Tolkachev 197 Jul 22, 2022
Make your church sermons available for download. For the latest version, go:

Laravel Church Sermons App Laravel church sermons app is basically an app for churches to make available the messages preached in church for all membe

Dammy 28 Nov 13, 2022
Relational Metrics - lararvel package help you to make your metrics easier

Relational Metrics This package will help you to make your metrics easier, You could get metrics about your Models, Models depending on their relation

Syrian Open Source 25 Oct 12, 2022
Laravel User Activity Log - a package for Laravel 8.x that provides easy to use features to log the activities of the users of your Laravel app

Laravel User Activity Log - a package for Laravel 8.x that provides easy to use features to log the activities of the users of your Laravel app

null 9 Dec 14, 2022