PHP code example of statikbe / laravel-cookie-consent

1. Go to this page and download the library: Download statikbe/laravel-cookie-consent library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

statikbe / laravel-cookie-consent example snippets


->withMiddleware(function (Middleware $middleware) {
    $middleware->web(append: [
        \Statikbe\CookieConsent\CookieConsentMiddleware::class,
    ]);
})

->withMiddleware(function (Middleware $middleware) {
    $middleware->alias([
        'cookie-consent' => \Statikbe\CookieConsent\CookieConsentMiddleware::class,
    ]);
})

protected $middlewareGroups = [
    'web' => [
        // ...
        \Statikbe\CookieConsent\CookieConsentMiddleware::class,
    ],
];

// app/Http/Kernel.php
protected $routeMiddleware = [
    'cookie-consent' => \Statikbe\CookieConsent\CookieConsentMiddleware::class,
];

// routes/web.php
Route::middleware('cookie-consent')->group(function () {
    // ...
});

return [
    /*
     * Theme for the cookie consent popup.
     * Options: 'default', 'filament'
     */
    'theme' => 'default',

    /*
     * Filament render hook used to register the cookie settings nav item.
     * Set to null to disable the nav item entirely.
     * See: Filament Integration > Nav item
     */
    'filament-nav-item-render-hook' => \Filament\View\PanelsRenderHook::USER_MENU_PROFILE_AFTER,

    /*
     * Name of the cookie written to the browser.
     * If you change this, update the GTM variable name to match.
     */
    'cookie_key' => '__cookie_consent',

    /*
     * Values written to the cookie for each consent choice.
     *
     *   analytics only  => '2'
     *   marketing only  => '3'
     *   both accepted   => 'true'
     *   none accepted   => 'false'
     *
     * GTM reads this value with regex triggers to decide which tags fire.
     * If you change these values, update your GTM triggers to match.
     */
    'cookie_value_analytics' => '2',
    'cookie_value_marketing' => '3',
    'cookie_value_both' => 'true',
    'cookie_value_none' => 'false',

    'cookie_expiration_days' => '365',

    /*
     * GTM custom event fired after the visitor saves their preferences.
     */
    'gtm_event' => 'cookie_refresh',

    /*
     * Relative paths where the cookie banner is suppressed.
     * Accepts wildcards via Str::is() — e.g. '/api/*', '/en/cookie-policy'.
     */
    'ignored_paths' => [],

    /*
     * Set to true to suppress the banner on 4xx/5xx error pages.
     */
    'skip_on_error_responses' => false,

    /*
     * Mark the consent cookie as Secure (HTTPS only).
     * Reads from the COOKIE_CONSENT_SECURE env variable.
     */
    'cookie_secure' => env('COOKIE_CONSENT_SECURE', false),

    /*
     * Cookie policy page URLs shown in the banner.
     * Read from env — add only the locales your site supports.
     */
    '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),
];

'ignored_paths' => ['/en/cookie-policy', '/api/documentation*'],

'skip_on_error_responses' => true,

// config/cookie-consent.php
'theme' => 'filament',

'filament-nav-item-render-hook' => \Filament\View\PanelsRenderHook::SIDEBAR_NAV_END,

'filament-nav-item-render-hook' => null,
bash
php artisan vendor:publish --provider="Statikbe\CookieConsent\CookieConsentServiceProvider" --tag="cookie-public"
bash
php artisan vendor:publish --provider="Statikbe\CookieConsent\CookieConsentServiceProvider" --tag="cookie-config"
css
@source 'vendor/statikbe/laravel-cookie-consent/resources/**/*.blade.php';
js
export default {
    content: [
        'vendor/statikbe/laravel-cookie-consent/resources/**/*.blade.php',
    ]
}
bash
php artisan vendor:publish --provider="Statikbe\CookieConsent\CookieConsentServiceProvider" --tag="cookie-lang"
bash
php artisan vendor:publish --provider="Statikbe\CookieConsent\CookieConsentServiceProvider" --tag="cookie-views"