PHPackages                             elegantly/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. elegantly/laravel-cookies-consent

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

elegantly/laravel-cookies-consent
=================================

Cookie consent for Laravel

v3.0.0(7mo ago)233.5k↓25%1[4 PRs](https://github.com/ElegantEngineeringTech/laravel-cookies-consent/pulls)MITPHPPHP ^8.2CI passing

Since Jul 14Pushed 1mo ago1 watchersCompare

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

READMEChangelog (10)Dependencies (13)Versions (23)Used By (0)

Laravel Cookies Consent Manager
===============================

[](#laravel-cookies-consent-manager)

[![Latest Version on Packagist](https://camo.githubusercontent.com/f13ce819a99d702f36313bb8dc516658e35718247f51155a1effd15c355023d6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f656c6567616e746c792f6c61726176656c2d636f6f6b6965732d636f6e73656e742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/elegantly/laravel-cookies-consent)[![GitHub Tests Action Status](https://camo.githubusercontent.com/220693c2125c642058e3ab4d804d4f65731b03eb749c4d2eb0907c0d1db6de08/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f456c6567616e74456e67696e656572696e67546563682f6c61726176656c2d636f6f6b6965732d636f6e73656e742f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/ElegantEngineeringTech/laravel-cookies-consent/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/9da0715515bfb3fed139d4e12eece69e24cb0a8638956cb23772b8421724dda4/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f456c6567616e74456e67696e656572696e67546563682f6c61726176656c2d636f6f6b6965732d636f6e73656e742f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/ElegantEngineeringTech/laravel-cookies-consent/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/d93f32ec45ef6346daed174e831e7509b1204f135b68b6045e8af7234bc529dd/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f656c6567616e746c792f6c61726176656c2d636f6f6b6965732d636f6e73656e742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/elegantly/laravel-cookies-consent)

[![laravel-cookies-consent](https://repository-images.githubusercontent.com/828522335/63d6882a-3b8f-4229-88d6-1e2f0d2eacee)](https://repository-images.githubusercontent.com/828522335/63d6882a-3b8f-4229-88d6-1e2f0d2eacee)

This package provides a simple yet extremely flexible way to manage cookie consent in your Laravel application.

The default cookie banner design requires Tailwind CSS and Alpine.js, but you can publish the component and customize it with your own stack.

[Demo here](https://elegantly.dev/laravel-cookies-consent)

Requirements
------------

[](#requirements)

### Backend

[](#backend)

- Laravel

### Frontend

[](#frontend)

The default cookie consent banner included in this package requires:

- Blade components
- [Alpine.js](https://alpinejs.dev/)
- [Tailwind CSS](https://tailwindcss.com/)
- [js-cookie](https://github.com/js-cookie/js-cookie)

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

[](#installation)

You can install the package via Composer:

```
composer require elegantly/laravel-cookies-consent
```

You can publish the config file with:

```
php artisan vendor:publish --tag="cookies-consent-config"
```

This is the content of the published config file:

```
return [

    /*
    |--------------------------------------------------------------------------
    | URL Configuration
    |--------------------------------------------------------------------------
    |
    | These values determine the package's API route URLs. Both values are
    | nullable and represent the same concepts as Laravel's routing parameters.
    |
    */

    'url' => [
        'domain' => null,
        'prefix' => 'cookiesconsent',
    ],

    /*
    |--------------------------------------------------------------------------
    | Consent Cookie Configuration
    |--------------------------------------------------------------------------
    |
    | To keep track of the user's preferences, this package stores
    | an anonymized cookie. You do not need to register this cookie in the
    | package's cookie manager as it is done automatically (under "essentials").
    |
    | The duration parameter represents the cookie's lifetime in minutes.
    |
    | The domain parameter, when defined, determines the cookie's activity domain.
    | For multiple sub-domains, prefix your domain with "." (e.g., ".mydomain.com").
    |
    */

    'cookie' => [
        'name' => Str::slug(env('APP_NAME', 'laravel'), '_').'_cookiesconsent',
        'lifetime' => 60 * 24 * 365,
        'domain' => null,
    ],

    /*
    |--------------------------------------------------------------------------
    | Legal Page Configuration
    |--------------------------------------------------------------------------
    |
    | Most cookie notices display a link to a dedicated page explaining
    | the extended cookies usage policy. If your application has such a page,
    | you can add its route name here.
    |
    */

    'policy' => null,

];
```

Usage
-----

[](#usage)

This package covers both backend and frontend cookie consent management.

You can choose to use the package only for backend capabilities or for both.

Backend Usage
-------------

[](#backend-usage)

In the backend, you will register the cookies and a callback associated with each of them. This callback will be a JavaScript script to run when the consent is granted.

### Register Your Cookies

[](#register-your-cookies)

First, you should register all the cookies requiring user consent.

To manage cookies, the package provides a service accessible via the Facade: `Elegantly\CookiesConsent\Facades\CookiesConsent`.

Cookie registration should be done in middleware to access the app and request context. This also allows you to choose the routes relying on those cookies.

To register your cookies, create a new middleware `App\Http\Middleware\RegisterCookiesConsent`. In this middleware, call `CookiesConsent::register` to register groups of cookies.

- Cookies are always registered in groups.
- A cookie is defined by its `name`, `lifetime`, and an optional `description`.
- A cookie group can be defined as `required`. Such cookies cannot be rejected by the user, which is useful for essential cookies like the session cookie.

For example, all cookies related to "Marketing" can be registered together:

```
namespace App\Http\Middleware;

use Carbon\CarbonInterval;
use Closure;
use Elegantly\CookiesConsent\CookieDefinition;
use Elegantly\CookiesConsent\CookieGroupDefinition;
use Elegantly\CookiesConsent\Facades\CookiesConsent;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class RegisterCookiesConsent
{
    public function handle(Request $request, Closure $next): Response
    {
        // Register cookies related to the Facebook pixel
        CookiesConsent::register(new CookieGroupDefinition(
            key: 'marketing',
            name: __('cookies-consent::cookies.marketing.name'),
            description: __('cookies-consent::cookies.marketing.description'),
            items: [
                new CookieDefinition(
                    name: '_fbc',
                    lifetime: CarbonInterval::years(2),
                    description: __('cookies-consent::cookies._fbc.description')
                ),
                new CookieDefinition(
                    name: '_fbp',
                    lifetime: CarbonInterval::years(3),
                    description: __('cookies-consent::cookies._fbp.description')
                ),
            ],
            onAccepted: function () {
                return
