Skip to content

atlasconsulting/cakephp-cookie-consent

Repository files navigation

CookieConsent plugin for CakePHP

php image image

This plugin helps to remove cookies your app set and on which a user didn't consent. It works reading a configurable cookie that contain information about the categories of cookie accepted by the user. It works out of the box with Cookie Consent.

Installation

You can install this plugin into your CakePHP application using composer.

The recommended way to install composer packages is:

composer require atlasconsulting/cakephp-cookie-consent

Usage

Add the CookieCosentMiddleware to the application middleware queue.

public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
{
    return $middlewareQueue
        // Catch any exceptions in the lower layers,
        // and make an error page/response
        ->add(new ErrorHandlerMiddleware(Configure::read('Error')))

        ->add(new CookieConsentMiddleware([
            'remove' => [
                'preferences' => ['lang'], // remove `lang` cookie if `preferences` category isn't accepted
                'analytics' => ['my_analytics'], // remove `my_analytics` cookie if `analytics` category isn't accepted
            ],
        ]));

        // other middlewares here
}

Configuration

The middleware is configurable with:

[
    'cookieName' => 'cc_cookie',
    'searchIn' => 'level',
    'remove' => [
        'preferences' => [],
        'analytics' => [],
        'targeting' => [],
    ],
]

where

  • cookieName (default cc_cookie), the cookie consent to analyze
  • searchIn (default level), the key to look for in the json string value of cookie. The value must be an array of cookies' categories accepted by the user, for example ['preferences', 'analytics'].
  • remove an array divided by cookie categories that are to remove if not accepted