Laravel 12 - How to Add Cookie Consent Using Statikbe Package

Touseef Afridi
03 Jul 25

Laravel 12 - How to Add Cookie Consent Using Statikbe Package

In the tutorial, we will learn how to add a GDPR-compliant cookie consent banner in Laravel 12 using the Statikbe package. A simple and effective way to ensure privacy compliance on your Laravel website.


If you're a video person, feel free to skip the post and check out the video instead!


Quick Overview

This guide covers how to integrate a cookie consent banner into a Laravel 12 application using the Statikbe Cookie Consent package. Whether you're working on a fresh install or updating an existing project, you'll start by installing the package via Composer. After that, you'll publish the required assets to include the necessary CSS and JavaScript for styling the banner. The next steps involve updating your main Blade layout to display the banner globally, registering the middleware in AppServiceProvider.php, and ensuring the styles are properly loaded. Once everything is set up, you can launch the development server to test the banner. The guide also walks through how to customize the consent message using localization files and fine-tune the behavior through the config/cookie-consent.php configuration file.

Step # 1 : Create fresh Laravel project or use existing project.

You can either work with your current Laravel 12 project or spin up a new one. If you have Laravel installed globally, use.
laravel new cookie
Or use Composer to create the project.
composer create-project laravel/laravel --prefer-dist cookie
During the interactive setup, choose the following options to keep things minimal and ready for customization.
  • Starter kit — Skip by selecting None.
  • Testing framework — Go with Pest for simplicity.
  • Database — Choose MySQL.
  • Run default migrations — Confirm with yes.
  • Run npm install and build assets — Proceed with yes.

This step prepares a clean Laravel 12 project named cookie with minimal scaffolding. By choosing None for the starter kit, you're keeping the setup lightweight. Pest is selected as the testing framework for a modern testing experience, MySQL is configured as the default database, and running the migrations and frontend build ensures your project is ready for development.

Step # 2 : Navigate to your project directory.

Open your terminal (such as Git Bash or CMD) and move into your Laravel project's root directory.
cd c:xampp/htdocs/cookie
This puts you inside the project folder so you can run Artisan commands, set configurations, and begin working on your Laravel app without any path-related issues.

Step # 3 : Add the Cookie Consent package to your project.

To bring cookie consent functionality into your Laravel 12 app, run the following command.
composer require statikbe/laravel-cookie-consent
The package sets itself up automatically after installation, no manual registration needed. It offers a pre-built, customizable banner to help your site stay compliant with cookie laws right out of the box.

Step # 4 : Publish the package assets.

Run the command below to publish the front-end assets required for the cookie banner.
php artisan vendor:publish --provider="Statikbe\CookieConsent\CookieConsentServiceProvider" --tag="cookie-public"
This will publish the necessary JavaScript and CSS files, ensuring that the banner appears correctly styled and behaves as expected on the frontend of your Laravel 12 application.

Step # 5 : Set up a Master layout.

To make sure the cookie consent banner appears across all pages, create a master Blade template with a clean layout and Tailwind CSS styling. This layout will act as the foundation for your Laravel views and ensure consistent appearance of the consent modal site-wide.
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Laravel 12 — Cookie Consent</title>
  <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
</head>
<body class="bg-gray-900 text-gray-100 min-h-screen flex flex-col">
  <!-- Header -->
  <header class="bg-gray-800 border-b border-gray-700">
    <div class="max-w-7xl mx-auto px-6 py-4 flex justify-between items-center">
      <h1 class="text-2xl font-bold text-white">🍪 Laravel Cookie Consent</h1>
    </div>
  </header>
  <!-- Main Content -->
  <main class="flex-grow flex items-center justify-center px-6">
    <div class="container mx-auto px-6 py-16 text-center">
      <h1 class="text-4xl font-bold text-white mb-6">Welcome to Code Shotcut!</h1>
      <p class="text-lg text-gray-400 mb-6">
        This is a demo page showing how to integrate the <strong>Laravel Cookie Consent</strong> package in a Laravel 12 project.
      </p>
      <p class="text-base text-gray-300 mb-4">
        To get started, install the package using Composer and publish the assets. Once added, the cookie banner will appear automatically across your application when using this layout.
      </p>
      <p class="text-base text-gray-300 mb-4">
        Make sure your base Blade layout includes the required CSS and JavaScript files, and that middleware is properly configured in your web routes.
      </p>
      <p class="text-base text-gray-300">
        For detailed documentation, visit the
        <a href="https://github.com/statikbe/laravel-cookie-consent"
           class="text-indigo-400 hover:underline" target="_blank">
          official GitHub repo
        </a>.
      </p>
    </div>
  </main>
  <!-- Footer -->
  <footer class="bg-gray-800 border-t border-gray-700 text-center py-4 text-sm text-gray-500">
    &copy; {{ date('Y') }} Code Shotcut. All rights reserved.
  </footer>
</body>
</html>
This master view provides a clean, centered layout with Tailwind styling and serves as the backbone of your application. By including the necessary styles and scripts here, the cookie consent modal will seamlessly display wherever this layout is used.

Step # 6 : Update Route.

In your web.php routes file, define a route to load the master Blade template as the homepage.
Route::get('/', function () {
    return view('master');
});
This setup ensures that when someone visits your website’s root URL, the master.blade.php view is rendered, which means the cookie consent banner will appear as expected across the application.

Step # 7 : Include Styles and Register Middleware.

To style the cookie banner correctly, add the following line inside your master.blade.php file.
<link rel="stylesheet" type="text/css" href='{{asset("vendor/cookie-consent/css/cookie-consent.css")}}'>
Next, open AppServiceProvider.php and update the boot() method to register the middleware globally.
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Statikbe\CookieConsent\CookieConsentMiddleware;
class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        //
    }
    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        // Add middleware to the 'web' group
        app('router')->pushMiddlewareToGroup('web', CookieConsentMiddleware::class);
    }
}
This makes sure the cookie consent logic is applied automatically to all routes in the web middleware group.

Step # 8 : Run the Project.

Start the Laravel development server using the command below.
php artisan serve
Once it's running, open your browser and go to: 127.0.0.1:8000. You should now see dark-themed layout along with the cookie consent banner, ready to go.

Step # 9 : Customize the Cookie Consent Banner.

Modifying Cookie Consent Messages :
To change the default cookie consent messages, run the command below.
php artisan vendor:publish --provider="Statikbe\CookieConsent\CookieConsentServiceProvider" --tag="cookie-lang"
This will publish the language files to lang/vendor/cookie-consent, where you can modify the text in different languages.

To edit the accept button text, open the file at lang/vendor/cookie-consent/en/text.php and update it like this.
From :
'alert_accept' => 'Accept all cookies',
To :
'alert_accept' => 'Custom Message - Accept all cookies',

This allows you to tailor the messaging to fit your app’s tone and branding.

Configuration of Cookie Consent :
To publish the configuration file for the package, use the command below.
php artisan vendor:publish --provider="Statikbe\CookieConsent\CookieConsentServiceProvider" --tag="cookie-config"
This will create config/cookie-consent.php, which includes various settings for managing cookie consent.
<?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' => 'cookie_refresh',
    'ignored_paths' => [],
    'cookie_secure' => env('COOKIE_CONSENT_SECURE', false),
    '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),
];
These options let you define cookie types, expiration period, and localized privacy policy links based on your application's needs.

Conclusion

By following this guide, you've successfully added the Laravel Cookie Consent package to your application. Your site now features a working consent banner that helps meet cookie compliance standards. This package streamlines the setup with built-in middleware, editable message files, and a flexible configuration system. You can take it a step further by customizing the text, fine-tuning cookie behavior, and aligning the design with your brand style.
For additional options and details, visit the official statikbe/laravel-cookie-consent documentation.
Share this with friends!


"Give this post some love and slap that 💖 button as if it owes you money! 💸😄"
0

0 Comments

To engage in commentary, kindly proceed by logging in or registering