PHPackages                             scify/laravel-cookies-consent - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. scify/laravel-cookies-consent

Abandoned → [scify/laravel-cookie-guard](/?search=scify%2Flaravel-cookie-guard)Library[Utility &amp; Helpers](/categories/utility)

scify/laravel-cookies-consent
=============================

A Laravel package that provides a Cookies consent floating window and publishes the Cookies for the different consent categories

v4.1.12(2mo ago)13726.2k↓36.6%4MITPHPPHP ^8CI passing

Since Nov 18Pushed 1mo ago2 watchersCompare

[ Source](https://github.com/scify/laravel-cookie-guard)[ Packagist](https://packagist.org/packages/scify/laravel-cookies-consent)[ Docs](https://github.com/scify/laravel-cookie-guard)[ RSS](/packages/scify-laravel-cookies-consent/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (26)Versions (53)Used By (0)

Laravel Cookie Guard - Make your Laravel app compliant with the EU GDPR cookie law
==================================================================================

[](#laravel-cookie-guard---make-your-laravel-app-compliant-with-the-eu-gdpr-cookie-law)

The **last** Cookies Consent Plugin your **Laravel app** will ever need!

[![logo](readme-images/Laravel_Cookie_Guard.png)](readme-images/Laravel_Cookie_Guard.png)

---

[![Latest Version on Packagist](https://camo.githubusercontent.com/eb6399f462e77cf9a328bde41b86ffa6815c1b3d96df872ec3083c3db8490b5d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f73636966792f6c61726176656c2d636f6f6b69652d67756172642e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/scify/laravel-cookie-guard)[![GitHub Tests Action Status](https://camo.githubusercontent.com/459352d647cb06286169f77b98a7e321e594fec88f4da650c10597702c25fbdf/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f73636966792f6c61726176656c2d636f6f6b69652d67756172642f72756e2d74657374732e796d6c3f6272616e63683d6d61696e)](https://github.com/scify/laravel-cookie-guard/actions?query=workflow%3Arun-tests+branch%3Amain)[![codecov](https://camo.githubusercontent.com/874b9a100f3dba5c9ce05f67a2c95f340c5b0a972f39fbbeb672a14e7980bec0/68747470733a2f2f636f6465636f762e696f2f67682f73636966792f6c61726176656c2d636f6f6b69652d67756172642f67726170682f62616467652e737667)](https://codecov.io/gh/scify/laravel-cookie-guard)[![Total Downloads](https://camo.githubusercontent.com/a55003454544b9cf5bbec39ca4c75dede72603dde2030443cb9749a0e80361c9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f73636966792f6c61726176656c2d636f6f6b69652d67756172642e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/scify/laravel-cookie-guard)[![GitHub Issues](https://camo.githubusercontent.com/7c3a4d35d0cb7a3cf7f16483d7958470d59eeb91996a1f2eaa28e9bfcb0cd3c7/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732f73636966792f6c61726176656c2d636f6f6b69652d6775617264)](https://img.shields.io/github/issues/scify/laravel-cookie-guard)[![GitHub Stars](https://camo.githubusercontent.com/e8cb1428ef12c1899f704b0ba99a680d4bcedd230854c18525c93c02a56b3a90/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f73636966792f6c61726176656c2d636f6f6b69652d6775617264)](https://img.shields.io/github/stars/scify/laravel-cookie-guard)[![GitHub forks](https://camo.githubusercontent.com/f74bc2a099b41dcd7943de80ba23151a4873b489fdf382d22e55774a8e8aef96/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f666f726b732f73636966792f6c61726176656c2d636f6f6b69652d6775617264)](https://img.shields.io/github/forks/scify/laravel-cookie-guard)[![contributions welcome](https://camo.githubusercontent.com/9e93e892d0685e1bf7a1d0bd7c8410d6ecf2086a0a7b48dd58a6b96fa556ea2a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f636f6e747269627574696f6e732d77656c636f6d652d627269676874677265656e2e7376673f7374796c653d666c6174)](https://github.com/dwyl/esta/issues)[![License](https://camo.githubusercontent.com/a549a7a30bacba7bfceebdc207a8e86c3f2c02995a2527640dca30048fd2b64e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d417061636865253230322e302d626c75652e737667)](https://opensource.org/licenses/Apache-2.0)[![Maintenance](https://camo.githubusercontent.com/5ca62441414bacaa54c6c6e5b68e46c76305947b6bf498c4949fc71c1b4b10dd/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4d61696e7461696e65642533462d7965732d677265656e2e737667)](https://GitHub.com/Naereen/StrapDown.js/graphs/commit-activity)[![Ask Me Anything !](https://camo.githubusercontent.com/f58000eb7d904f735fbbca30835a588c716bdde0781156f1fb2e601d44307a3a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f41736b2532306d652d616e797468696e672d3161626339632e737667)](https://GitHub.com/scify)[![GitHub contributors](https://camo.githubusercontent.com/16e1f4c8a17d7233aa04fddd25403cac3da0b2515a27d99a1779396cb92f40da/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f636f6e7472696275746f72732f73636966792f6c61726176656c2d636f6f6b69652d6775617264)](https://github.com/scify/laravel-cookie-guard/graphs/contributors)[![GitHub pull-requests](https://camo.githubusercontent.com/a340c102d96cd10129945e5a8c67be466e33fb6e33186b87b28e9769d9dae728/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732d70722f73636966792f6c61726176656c2d636f6f6b69652d6775617264)](https://github.com/scify/laravel-cookie-guard/pulls)[![GitHub closed pull-requests](https://camo.githubusercontent.com/692d38942f05e1f807099bf3213556d49e9b685c19f956c5dba3a771ff41711b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732d70722d636c6f7365642f73636966792f6c61726176656c2d636f6f6b69652d6775617264)](https://github.com/scify/laravel-cookie-guard/pulls?q=is%3Apr+is%3Aclosed)

Table of Contents
-----------------

[](#table-of-contents)

- [Laravel Cookie Guard - Make your Laravel app compliant with the EU GDPR cookie law](#laravel-cookie-guard---make-your-laravel-app-compliant-with-the-eu-gdpr-cookie-law)
    - [Table of Contents](#table-of-contents)
    - [Upgrading to v4](#upgrading-to-v4)
    - [About the plugin](#about-the-plugin)
    - [Features](#features)
    - [Installation](#installation)
        - [SEO](#seo)
        - [Explanation of the configuration file](#explanation-of-the-configuration-file)
    - [Usage](#usage)
        - [Option 1: All-in-one dialog (default)](#option-1-all-in-one-dialog-default)
        - [Option 2: Simple dialog, with a link to the default separate internal page](#option-2-simple-dialog-with-a-link-to-the-default-separate-internal-page)
        - [Option 3: Simple dialog, with a link to a customized separate internal page with navbars, footers, etc.](#option-3-simple-dialog-with-a-link-to-a-customized-separate-internal-page-with-navbars-footers-etc)
        - [Option 4: Simple dialog, with a link to an external (off-the-app) page](#option-4-simple-dialog-with-a-link-to-an-external-off-the-app-page)
    - [How to override the CSS styles](#how-to-override-the-css-styles)
    - [How to add a new cookie category](#how-to-add-a-new-cookie-category)
    - [How to check if a cookie category is allowed](#how-to-check-if-a-cookie-category-is-allowed)
        - [Backend code](#backend-code)
        - [Frontend code](#frontend-code)
    - [Customization](#customization)
        - [Customizing the component texts](#customizing-the-component-texts)
        - [Customizing the component contents](#customizing-the-component-contents)
    - [Development](#development)
        - [Testing](#testing)
    - [FAQ](#faq)
    - [Changelog](#changelog)
    - [Contributing](#contributing)
        - [PHP code style - Laravel Pint](#php-code-style---laravel-pint)
        - [Releasing a new version](#releasing-a-new-version)
    - [Credits](#credits)
    - [License](#license)

Upgrading to v4
---------------

[](#upgrading-to-v4)

As of **March 2025**, The `v4` of the Laravel Cookies Consent plugin has been released! 🎉🥳😍 It offers a **new, revamped UI**, and a lot of new features and improvements.

**Important:**Since the plugin now has a completely new name, you will need to uninstall the old package, and install the new one.

```
rm -rf public/vendor/scify && rm -rf vendor/scify

composer remove scify/laravel-cookies-consent

composer require scify/laravel-cookie-guard

php artisan vendor:publish --provider="SciFY\LaravelCookiesConsent\LaravelCookiesConsentServiceProvider" --tag="cookies-consent-public" --force
```

Then, make sure to check the configuration file `config/cookies_consent.php` and update it according to [the new one](config/cookies_consent.php).

Then, make sure that the Laravel components you use are the new ones:

Intead of using:

```

```

Use:

```

```

In order to upgrade to `v4`, please follow the instructions in the [CHANGELOG](CHANGELOG.md) file, and consult also the [How To Upgrade to a New Major Version](UPGRADING.md) file.

Then, make sure to publish the assets, the translations, and the configuration file, and update your Blade files

About the plugin
----------------

[](#about-the-plugin)

According to the [GDPR law](https://gdpr-info.eu/), every platform is required to allow the users to decide which cookie categories they will allow, and, if a cookie category is not allowed, the application should not use the functionality tied to that cookie.

This plugin provides a simple cookie consent window through which the user can specify the cookies they would like to allow.

After the user submission, the page reloads and the relevant cookies are set on the browser, and can then be used in the front-end.

Features
--------

[](#features)

- Customizable cookie categories
- 3 Different integration options: All-in-one dialog, internal page, external separate page,
- A clean and intuitive UI for the cookies consent modal.
- A sticky cookies button and footer link that allows users to change their cookies preferences at any time. This button is optional, and it's existence can be tweaked in the configuration file.
- A separate page for the cookies preferences, where users can read more about each cookie category and change their preferences.
- Multilingual support. The plugin comes with 6 built-in languages: English, Greek, Spanish, German, Italian, and Swedish. You can add your own language by publishing the translations and editing/adding your own translations.

Installation
------------

[](#installation)

1. You can install the package via composer:

```
composer require scify/laravel-cookie-guard
```

2. Then, **Make sure to manually publish** the cookie image file, by running:

```
php artisan vendor:publish \
--provider="SciFY\LaravelCookiesConsent\LaravelCookiesConsentServiceProvider" \
--tag="cookies-consent-public"
```

**Notice:** if a new version of the plugin has been released, and you want to update the front-end files, please make sure to use `--force` option, to overwrite the file.

By doing so, the image file will be copied to `public/vendor/scify/laravel-cookie-guard`.

**IMPORTANT:**

You can then either decide to include the `public/vendor/scify/laravel-cookie-guard/*` files to git (especially if you want to edit it first), or add it to `.gitignore`, and make sure to also run this command on the staging/production server.

Next, you need to publish the config file:

```
php artisan vendor:publish \
--provider="SciFY\LaravelCookiesConsent\LaravelCookiesConsentServiceProvider" \
--tag="cookies-consent-config"
```

The configuration file will be published to `config/cookies_consent.php`.

3. Use the provided Laravel View Components

In order to use the plugin, you need to include the Laravel View Components in your Blade files, where you want the cookies to appear (typically just before the closing `` tag):

```

```

### SEO

[](#seo)

Tip: Depending on the HTML structure of your app, you can specify the `heading` and `accordion-heading` attributes, so that the SEO rules are respected:

```

```

This will impact the heading level of the "We value your privacy" dialog title, and the "Read More" accordion titles.

### Explanation of the configuration file

[](#explanation-of-the-configuration-file)

In the config file, you can change the cookie categories of your application, set the required and pre-selected categories, as well as add new categories.

This is the contents of the published config file:

```
return [
    /**
     * This prefix will be applied when setting and getting all cookies.
     * If not set, the cookies will not be prefixed.
     * If set, a good strategy is to also add a trailing underscore "_", that will be added between the field value, and each cookie.
     * For example, if `cookie_prefix` is set to `my_app_`, then the targeting cookie will have a value of `my_app_cookies_consent_targeting`.
     * When using this plugin for multiple apps, it is a good strategy to set a prefix that is relevant to the app
     * (for example "my_app_", in order for the cookies not to be mingled when running locally.
     */
    'cookie_prefix' => '',
    'display_floating_button' => true, // Set to false to display the footer link instead
    'hide_floating_button_on_mobile' => false, // Set to true to hide the floating button on mobile
    'use_separate_page' => false, // Set to true to use a separate page for cookies explanation
    'categories_collapsed_by_default' => true, // Set to false to initially collapse the optional categories
    'use_show_modal' => true, // Set to false to disable the use of showModal() on the dialog HTML element
    /*
    |--------------------------------------------------------------------------
    | Editor
    |--------------------------------------------------------------------------
    |
    | Choose your preferred cookies to be shown. You can add more cookies as desired.
    | If, for example you add another cookie with the name "marketing", then you should also
    | publish the translation files and add a "cookie_marketing" key in the translation file,
    | since the plugin will try to display the cookie name by this convention.
    |
    | Built-in: "strictly_necessary"
    |
    */
    'cookies' => [
        'strictly_necessary' => [
            [
                // you need to change this in order to reflect the cookie_prefix from above
                'name' => 'my_app_cookies_consent',
                'description' => 'cookies_consent::messages.cookie_cookies_consent_description',
                'duration' => 'cookies_consent::messages.years',
                'duration_count' => 2,
                'policy_external_link' => null,
            ],
            [
                'name' => 'XSRF-TOKEN',
                'description' => 'cookies_consent::messages.cookie_xsrf_token_description',
                'duration' => 'cookies_consent::messages.hours',
                'duration_count' => 2,
                'policy_external_link' => null,
            ],
            [
                'name' => 'laravel_session',
                'description' => 'cookies_consent::messages.cookie_laravel_session_description',
                'duration' => 'cookies_consent::messages.hours',
                'duration_count' => 2,
                'policy_external_link' => null,
            ],
        ],
    ],
    'enabled' => [
        'strictly_necessary',
    ],
    'required' => ['strictly_necessary'],
    /*
     * Set the cookie duration in days.  Default is 365 days.
     */
    'cookie_lifetime' => 365,
];
```

The `cookie_prefix` is optional and, if set, will be applied to every cookie.

If set, a good strategy is to also add a trailing underscore "\_", that will be added between the field value, and each cookie.

For example, if `cookie_prefix` is set to `my_app_`, then the targeting cookie will have a value of `my_app_cookies_consent_targeting`.

The `display_floating_button` field is optional and, if set to `true`, will display a floating button in the bottom right corner of the page. If set to `false`, then you will need to add a relevant link in your footer, in order to show the cookies preferences dialog:

```

  {{ __('cookies_consent::messages.cookies_settings') }}

```

If you prefer opening the dialog by hash, you can use:

```

  {{ __('cookies_consent::messages.cookies_settings') }}

```

The `hide_floating_button_on_mobile` field is optional and, if set to `true`, will hide the floating button on mobile

The `use_separate_page` field is optional and, if set to `true`, will display the cookies preferences in a separate page.

The `categories_collapsed_by_default` field is optional and, if set to `false`, will initially collapse the optional categories. If set to `true`, all categories will be collapsed by default.

The `use_floating_modal` field is optional and, if set to `true`, will disable the use of `showModal()` on the dialog HTML, and the fully-accessible modal. It will show a floating window on the left, instead.

You can add as many cookie categories as you like, simply by adding values to the `cookies` array.

If you want to remove a cookie category, simply remove it from the array.

You can use the `enabled` array to set the cookie categories that will be pre-selected, and the `required` array to set the cookies that the user won't be able to deselect.

If you want to change how long the cookies will be stored, edit the `cookie_lifetime` variable.

Usage
-----

[](#usage)

### Option 1: All-in-one dialog (default)

[](#option-1-all-in-one-dialog-default)

**Suitable for**: Apps that would like to show a single dialog with all the cookie categories.

When the plugin is installed, a `laravel-cookie-guard`custom [Laravel View Component](https://laravel.com/docs/9.x/blade#components) is automatically registered.

This will render the following cookies consent that, will look very much like this one.

[![dialog 1](readme-images/dialog_1.png)](readme-images/dialog_1.png)

When the user cliks on "Customise", they will see the following dialog:

[![dialog 2](readme-images/dialog_2.png)](readme-images/dialog_2.png)

They then can open each cookies category and see the cookies:

[![dialog 3](readme-images/dialog_3.png)](readme-images/dialog_3.png)

You can then use this component in order to display the cookies consent window, wherever you'd like.

Typically, a good strategy is to put the component just before the closing `` tag:

```

...
...
...

```

(Or you can put the scripts in the `` tag, if you prefer).

### Option 2: Simple dialog, with a link to the default separate internal page

[](#option-2-simple-dialog-with-a-link-to-the-default-separate-internal-page)

**Suitable for**: Apps that would like to show a simple "Accept all/Reject Optional" dialog with a link to a separate page for customizing the cookies preferences.

Edit the `config/cookies_consent.php` file, and set the `use_separate_page` field to `true`.

```
    'use_separate_page' => true,
```

When the plugin is installed, a `/cookie-policy/{locale}` route is automatically registered. So, you can edit the `config/cookies_consent.php` file, and set the `use_separate_page` field to `true`.

Now, the user will see a simple dialog, with a link to the `/cookie-policy/{locale}` page, where they can customize their cookies preferences. This page is managed by the plugin, and it will show the cookies categories, descriptions, and the ability to accept or reject them.

### Option 3: Simple dialog, with a link to a customized separate internal page with navbars, footers, etc.

[](#option-3-simple-dialog-with-a-link-to-a-customized-separate-internal-page-with-navbars-footers-etc)

**Suitable for**: Apps that would like to show a simple "Accept all/Reject Optional" dialog with a link to a customized page, with the app's own content (navigation, footer, etc).

If you would like to show a simple "Accept all/Reject Optional" dialog with a link to a separate page for customizing the cookies preferences, you can set the following in the `config/cookies_consent.php` file:

```
'use_separate_page' => true,
'cookie_policy_page_custom_url' => '/my-custom-cookies-policy',
```

```
Route::get('/my-custom-cookies-policy', function () {
    return view('my-custom-cookies-policy');
});
```

Or you can declare your route to override the default one:

```
'use_separate_page' => true,
'cookie_policy_page_custom_url' => null,
```

And then create a new route in your `routes/web.php` file:

```
Route::get('/cookie-policy/{locale}', function () {
    return view('my-custom-cookies-policy');
});
```

Then, you can create a new route in your `routes/web.php` file:

Now, in the `resources/views/my-custom-cookies-policy.blade.php` file, you will need to render the `x-laravel-cookie-guard-page` component, in order to show the cookies preferences dialog.

Here is a usage example:

```
@extends('layouts.app')
@section('head)

@endsection
@section('content')

@endsection

```

Then, the user will see an appropriate dialog, with a link to the `/my-custom-cookies-policy` page:

[![dialog 4](readme-images/dialog_4.png)](readme-images/dialog_4.png)

### Option 4: Simple dialog, with a link to an external (off-the-app) page

[](#option-4-simple-dialog-with-a-link-to-an-external-off-the-app-page)

**Suitable for**: Apps that would like to show a simple "Accept all/Reject Optional" dialog, with a link to an external URL.

If you would like to show a simple "Accept all/Reject Optional" dialog with a link to an external URL, you can set the following in the `config/cookies_consent.php` file:

```
    'use_separate_page' => true,
    'cookie_policy_page_custom_url' => 'https://www.example.com/cookies-policy',
```

Then, the user will see an appropriate dialog, with a link to the external page.

How to override the CSS styles
------------------------------

[](#how-to-override-the-css-styles)

If you want to override the CSS styles of the cookies consent dialog, you can do this:

```

```

Then, you can add a `` tag in your Blade file, in order to override the CSS styles:

```

  #scify-cookies-consent, #scify-cookie-policy-page, #scify-cookie-consent-floating-button {
    --scify-cookies-primary-color: #ff5722; /* Override primary color */

    ...

    /* Add more override rules here */
  }

```

The full list of CSS variables that can be overridden can be found in the `public/vendor/scify/laravel-cookie-guard/_variables.css` file.

How to add a new cookie category
--------------------------------

[](#how-to-add-a-new-cookie-category)

In order to add a new cookie category, you need to add a new entry in the `cookies` array in the configuration file:

```
        'strictly_necessary' => [
            ...
            ...
        ],
        'targeting' => [
            [
                'name' => '_ga',
                'description' => 'This cookie is installed by Google Analytics. The cookie is used to calculate visitor, session, campaign data and keep track of site usage for the site\'s analytics report. The cookies store information anonymously and assign a randomly generated number to identify unique visitors.',
                'duration' => 'cookies_consent::messages.years',
                'duration_count' => 2,
                'policy_external_link' => 'https://policies.google.com/privacy?hl=en-US',
            ],
            [
                'name' => '_gid',
                'description' => 'This cookie is installed by Google Analytics. The cookie is used to store information of how visitors use a website and helps in creating an analytics report of how the website is doing. The data collected including the number visitors, the source where they have come from, and the pages visited in an anonymous form.',
                'duration' => 'cookies_consent::messages.days',
                'duration_count' => 1,
                'policy_external_link' => 'https://policies.google.com/privacy?hl=en-US',
            ],
            [
                'name' => '_gat',
                'description' => 'This cookies is installed by Google Universal Analytics to throttle the request rate to limit the colllection of data on high traffic sites.',
                'duration' => 'cookies_consent::messages.minutes',
                'duration_count' => 1,
                'policy_external_link' => 'https://policies.google.com/privacy?hl=en-US',
            ],
        ],
```

Or, you can set the `description` to be null (or an empty string), or set a translation key directly, like so:

1. Create a cookies-specific translation file:

```
 touch lang/en/cookies.php
```

Contents:

```
