https://github.com/elegantengineeringtech/laravel-cookies-consent
Extremly flexible Cookies consent manager for Laravel
https://github.com/elegantengineeringtech/laravel-cookies-consent
cookie-consent cookieconsent cookies-consent gdpr laravel laravel-package php tall-stack
Last synced: about 1 year ago
JSON representation
Extremly flexible Cookies consent manager for Laravel
- Host: GitHub
- URL: https://github.com/elegantengineeringtech/laravel-cookies-consent
- Owner: ElegantEngineeringTech
- License: mit
- Created: 2024-07-14T12:09:26.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2025-03-10T02:57:38.000Z (over 1 year ago)
- Last Synced: 2025-03-22T15:48:20.229Z (about 1 year ago)
- Topics: cookie-consent, cookieconsent, cookies-consent, gdpr, laravel, laravel-package, php, tall-stack
- Language: PHP
- Homepage: https://elegantengineering.tech/laravel-cookies-consent
- Size: 94.7 KB
- Stars: 10
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
Awesome Lists containing this project
README
# Laravel Cookies Consent Manager
[](https://packagist.org/packages/elegantly/laravel-cookies-consent)
[](https://github.com/ElegantEngineeringTech/laravel-cookies-consent/actions?query=workflow%3Arun-tests+branch%3Amain)
[](https://github.com/ElegantEngineeringTech/laravel-cookies-consent/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
[](https://packagist.org/packages/elegantly/laravel-cookies-consent)

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://elegantengineering.tech/laravel-cookies-consent)
## Requirements
### Backend
- Laravel
### 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
You can install the package via Composer:
```bash
composer require elegantly/laravel-cookies-consent
```
You can publish the config file with:
```bash
php artisan vendor:publish --tag="cookies-consent-config"
```
This is the content of the published config file:
```php
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
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
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
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:
```php
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 <<<'JS'
if (typeof fbq === 'function') {
fbq('consent', 'grant');
}
JS;
},
));
return $next($request);
}
}
```
### Registering Essential Cookies
The package provides a preset for essential cookies. Essential cookies are those that cannot be removed without compromising the application.
By default, Laravel includes 2 essential cookies:
- `XSRF-TOKEN`
- Session cookie
This package adds a third one:
- Consents (a cookie to store consents).
You can automatically register these three essential cookies using:
```php
use Elegantly\CookiesConsent\Facades\CookiesConsent;
CookiesConsent::registerEssentials()
->register(
// ... custom cookie definition
)
```
### Registering Cookie Callbacks
Using the `onAccepted` parameter, you can define the JavaScript code to execute when consent is granted to a specific cookie group.
In the previous example, we grant consent using the Facebook pixel.
```php
use Elegantly\CookiesConsent\Facades\CookiesConsent;
CookiesConsent::register(new CookieGroupDefinition(
// ...
onAccepted: function () {
return <<<'JS'
// This JavaScript code will be executed when consent is granted
if (typeof fbq === 'function') {
fbq('consent', 'grant');
}
JS;
},
));
```
## Frontend Usage
### Using the Default Cookie Banner
You can use the default cookie banner included with this package. It requires js-cookie, Alpine and tailwindcss.
#### js-cookie Requirement
The default banner implementation requires the [js-cookie](https://github.com/js-cookie/js-cookie) library to parse cookies in the browser.
Add it to your project using the CDN:
```html
```
Or see [their documentation](https://github.com/js-cookie/js-cookie) to install it via npm.
#### Alpine.js Requirement
The default banner implementation requires Alpine.js for reactivity. Ensure it is included in your page.
Simply put the banner component `` at the end of your HTML page, and you are ready to go!
```php