$ show whitecube/laravel-cookie-consent
This is a Laravel package that provides a complete, EU GDPR-compliant cookie consent management system. It allows developers to register cookies under predefined or custom categories (essential, analytics, optional), define cookie descriptions, durations, and consent callbacks. The package includes Blade directives and a Facade for easy integration into Laravel views and controllers, as well as a JavaScript library for enhanced UX with AJAX-based consent handling. The architecture is a typical Laravel package: service provider, routes, controllers, views, translations, and a compiled assets pipeline. The backend handles cookie registration, consent storage (via a cookie), and response generation (scripts, cookies). The frontend provides a customizable notice modal with toggleable details and buttons. The package is well-structured with clear separation of concerns using traits for shared functionality (attributes, callbacks, translations, cookie collections). It supports multiple languages and allows full customization of views, styles, and scripts. The codebase is actively maintained, supports Laravel 9-13, and includes a comprehensive test suite.
The LLM identified 4 files worth highlighting for reuse:
A trait that adds magic property accessors (__get, __set) and explicit getters/setters for arbitrary attributes. It is self-contained, can be used in any PHP class to provide dynamic attribute storage, and demonstrates clean implementation of the 'magic property' pattern with proper separation of setAttribute, getAttribute, setAttributes, getAttributes methods.
A trait that adds a consent callback mechanism. It stores a Closure, checks existence, and invokes it via Laravel's App::call, which resolves dependencies from the container. This pattern is reusable for any scenario where a deferred callback with dependency injection is needed, not just cookies. The trait is isolated and demonstrates a clean way to handle callbacks with Laravel's IoC.
A trait that manages a collection of Cookie or CookiesGroup instances. It provides methods to add individual cookies (via instance or Closure), retrieve all cookies as a flat array, and retrieve raw defined items. The trait is self-contained and could be extracted to manage any collection of similar objects. It demonstrates a clean pattern for aggregating items with different types (single vs group) and flattening.
A class that represents the result of a consent callback. It collects cookies (via Laravel's CookieFacade) and script tags to be added to the response. It is used by the HasConsentCallback trait to build the output. The class is self-contained, demonstrates a clean builder pattern for assembling response items, and is reusable in any consent-like scenario.