Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/statikbe/laravel-cookie-consent
The package includes a script & styling for a cookie banner and a modal where the visitor can select his/her cookie preferences.
https://github.com/statikbe/laravel-cookie-consent
Last synced: 7 days ago
JSON representation
The package includes a script & styling for a cookie banner and a modal where the visitor can select his/her cookie preferences.
- Host: GitHub
- URL: https://github.com/statikbe/laravel-cookie-consent
- Owner: statikbe
- License: mit
- Created: 2020-02-03T07:07:28.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2025-01-14T13:47:04.000Z (about 1 month ago)
- Last Synced: 2025-02-07T19:09:47.485Z (14 days ago)
- Language: PHP
- Homepage:
- Size: 2.24 MB
- Stars: 181
- Watchers: 12
- Forks: 27
- Open Issues: 6
-
Metadata Files:
- Readme: readme.md
- Changelog: changelog.md
- License: license.md
Awesome Lists containing this project
README
# Laravel cookie consent modal
[](https://packagist.org/packages/statikbe/llaravel-cookie-consent)
[](https://packagist.org/packages/statikbe/laravel-cookie-consent)

The package includes a script & styling for a cookie banner and a modal where the visitor can select his/her cookie preferences.
This package is mainly based on the one from spatie: https://github.com/spatie/laravel-cookie-consent
With the only exception that you can choose which cookies you enable.
This only works when Google Tag Manager is correctly configured (some regex config based on the value set in the cookie).* [Upgrading](upgrading.md)
* [Installation](#installation)
* [Usage](#usage)
* [Customising the dialog texts](#customising-the-dialog-texts)
+ [Customising the dialog contents](#customising-the-dialog-contents)
+ [Publishing](#publishing)
- [Config](#config)
- [Translations](#translations)
- [Views](#views)
* [Configure Google Tag Manager](#configure-google-tag-manager)
* [Security](#security)
* [License](#license)
## Upgrading
You can find our upgrading guides [here](upgrading.md).## Installation
You can install the package via composer:
``` bash
composer require statikbe/laravel-cookie-consent
```The package will automatically register itself.
First of all **you need to** publish the javascript and css files:
```bash
php artisan vendor:publish --provider="Statikbe\CookieConsent\CookieConsentServiceProvider" --tag="cookie-public"
```Include the css/cookie-consent.css into your base.blade.php or any other base template you use.
``````
The javascript file is included in the cookie snippet and will be added at the end of your body.
## UsageInstead of including a snippet in your view, we will automatically add it. This is done using middleware using two methods:
__For Laravel 11.x and newer__
```php
// bootstrap/app.php->withMiddleware(function (Middleware $middleware) {
...
$middleware->web(append: [
...
\Statikbe\CookieConsent\CookieConsentMiddleware::class,
]);// OR AS AN ALIAS
$middleware->alias([
...
'cookie-consent' => \Statikbe\CookieConsent\CookieConsentMiddleware::class,
]);
})```
__For Laravel 10.x and earlier__```php
// app/Http/Kernel.phpclass Kernel extends HttpKernel
{
protected $middleware = [
// ...
\Statikbe\CookieConsent\CookieConsentMiddleware::class,
];protected $routeMiddleware = [
// ...
'cookie-consent' => \Statikbe\CookieConsent\CookieConsentMiddleware::class,
];
// routes/web.php
Route::group([
'middleware' => ['cookie-consent']
], function(){
// ...
});
}
```This will add `cookieConsent::index` to the content of your response right before the closing body tag.
## Customising the dialog texts
If you want to modify the text shown in the dialog you can publish the lang-files with this command:
```bash
php artisan vendor:publish --provider="Statikbe\CookieConsent\CookieConsentServiceProvider" --tag="cookie-lang"
```This will publish this file to `resources/lang/vendor/cookieConsent/en/texts.php`.
```php
return [
'alert_title' => 'Deze website gebruikt cookies',
'setting_analytics' => 'Analytische cookies',
];
```
If you want to translate the values to, for example, English, just copy that file over to `resources/lang/vendor/cookieConsent/fr/texts.php` and fill in the English translations.
### Customising the dialog contentsIf you need full control over the contents of the dialog. You can publish the views of the package:
```bash
php artisan vendor:publish --provider="Statikbe\CookieConsent\CookieConsentServiceProvider" --tag="cookie-views"
```This will copy the `index` view file over to `resources/views/vendor/cookieConsent`.
The `cookie-settings` view file is just a snippet you need to place somewhere onto your page. Most preferably in the footer next to the url of your cookie policy.
```html
@lang('cookie-consent::texts.alert_settings')
```This gives your visitor the opportunity to change the settings again.
### Publishing
#### Config```bash
php artisan vendor:publish --provider="Statikbe\CookieConsent\CookieConsentServiceProvider" --tag="cookie-config"
```
This is the contents of the published config-file:
This will read the policy urls from your env.
```php
return [
'cookie_key' => '__cookie_consent',
'cookie_value_analytics' => '2',
'cookie_value_marketing' => '3',
'cookie_value_both' => 'true',
'cookie_value_none' => 'false',
'cookie_expiration_days' => '365',
'gtm_event' => 'pageview',
'ignored_paths' => [],
'policy_url_en' => env('COOKIE_POLICY_URL_EN', null),
'policy_url_fr' => env('COOKIE_POLICY_URL_FR', null),
'policy_url_nl' => env('COOKIE_POLICY_URL_NL', null),
];
```
You can customize some settings that work with your GTM.#### Don't show modal on cookie policy page or other pages
If you don't want the modal to be shown on certain pages you can add the relative url to the ignored paths setting. This also accepts wildcards (see the Laravel `Str::is()` [helper](https://laravel.com/docs/9.x/helpers#method-str-is)).
```
'ignored_paths => ['/en/cookie-policy', '/api/documentation*'];
```#### Translations
```bash
php artisan vendor:publish --provider="Statikbe\CookieConsent\CookieConsentServiceProvider" --tag="cookie-lang"
```#### Views
```bash
php artisan vendor:publish --provider="Statikbe\CookieConsent\CookieConsentServiceProvider" --tag="cookie-views"
```## Configure Google Tag Manager
All the steps to configure your Google Tag Manager can be found [here](docs/google-tag-manager.md).## Security
If you discover any security related issues, please email [[email protected]](mailto:[email protected]) instead of using the issue tracker.
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.