Laravel 12 – DevRabiul GDPR Cookie Consent Package.

Touseef Afridi
05 Mar 26

Laravel 12 – DevRabiul GDPR Cookie Consent Package.

In this tutorial, we will learn how to add a GDPR-compliant cookie consent banner in Laravel 12 using the DevRabiul package, including setup, master layout, home page, and full customization.

Quick Overview

This guide walks you through adding a GDPR compliant cookie consent banner to a Laravel 12 project using the DevRabiul Laravel Cookie Consent package. You start by creating a fresh Laravel application, installing the package, and publishing its configuration file for customization. A master layout is then created with Tailwind CSS, including the navbar, footer, and placeholders for the cookie consent styles and scripts, followed by a home page that extends this layout and a route set to load it at the root URL (/). By running the Laravel development server and visiting the home page, you can see the banner in action, which can be customized for themes, text, buttons, privacy policy links, and cookie categories, with all changes automatically applied across pages using the master layout.

Step # 1 : Spin Up a Brand New Laravel 12 Project.

To begin, we need a clean Laravel 12 installation. This ensures we’re working with a minimal and unmodified environment before integrating the cookie consent package. If you have the Laravel Installer available globally on your system, you can create a new project by running.
laravel new consent
Alternatively, if you prefer using Composer directly, run.
composer create-project laravel/laravel consent
When using Composer, Laravel generates the project immediately without asking any setup questions. You can configure everything afterward inside your .env file. If you choose the Laravel Installer, it will guide you through a short interactive setup process. During installation, use the following options.
  • Starter Kit: Select None for the starter kit so the project remains clean and unopinionated.
  • Testing Framework: Choose Pest as the testing framework for a modern and expressive testing experience.
  • Database: Select MySQL as the database driver.
  • Run Migrations: When prompted to run migrations, type Yes so the default tables are created immediately.
  • Frontend Dependencies: Choose Yes to install frontend dependencies automatically.

Once the installation completes, you’ll have a fresh Laravel 12 application named consent. The project will be configured with Pest for testing, MySQL as the database, frontend dependencies installed, and the default migrations already executed.

Step # 2 : Move Into Your Laravel Application Folder.

After creating the project, open your terminal and navigate into the newly generated folder by running.
cd consent
Make sure you're inside the project’s root directory before running any upcoming Composer or Artisan commands, as all further setup will be done from here.

Step # 3 : Install the Laravel Cookie Consent Package.

Next, we need to install the Laravel Cookie Consent package using Composer. Run the following command from the project directory.
composer require devrabiul/laravel-cookie-consent
Composer will automatically download the package and register it with the Laravel application. The Laravel Cookie Consent package allows websites to display a cookie consent banner, helping them comply with GDPR and other privacy regulations. Once installed, the banner can be customized with messages, buttons, and links to fit the needs of the application.

Step # 4 : Publish the Package Configuration.

To make the package configurable, publish its configuration file by running.
php artisan vendor:publish --provider="Devrabiul\CookieConsent\CookieConsentServiceProvider"
This will create the configuration file at config/cookie-consent.php, allowing you to customize the banner text, layout, themes, and cookie settings. The package’s CSS and JavaScript are included automatically and do not require separate publishing.

Step # 5 : Create a Master Layout.

We’re going to create a Master layout that serves as the foundation for all pages. This layout allows you to include global elements like the navbar, footer, and any scripts or styles such as a cookie consent banner so they automatically appear on every page that extends this layout. Create a new file at: resources/views/layouts/master.blade.php. Then add the following content.
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Laravel 12 - Code Shotcut DevRabiul Cookie Consent</title>
    <!-- Tailwind CSS -->
    <script src="https://cdn.tailwindcss.com"></script>
    <!-- Include compiled CSS and JS if using Vite -->
    @vite(['resources/css/app.css', 'resources/js/app.js'])
    <!-- DevRabiul / Laravel Cookie Consent Styles -->
    {!! CookieConsent::styles() !!}
</head>
<body class="bg-gray-950 text-gray-200 font-sans min-h-screen flex flex-col">
    <!-- Navbar -->
    <nav class="bg-gray-900 text-white shadow">
        <div class="max-w-6xl mx-auto px-4 py-4 flex justify-between items-center">
            <a href="{{ url('/') }}" class="text-lg font-semibold">Code Shotcut</a>
            <div class="space-x-4">
                <a href="#" class="hover:text-gray-300">Home</a>
                <a href="#" class="hover:text-gray-300">About</a>
                <a href="#" class="hover:text-gray-300">Contact</a>
            </div>
        </div>
    </nav>
    <!-- Main Content -->
    <main class="flex-grow max-w-6xl mx-auto px-4 py-8">
        @yield('content')
    </main>
    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-400 py-6 text-center text-sm">
        © Code Shotcut. All rights reserved.
    </footer>
    <!-- DevRabiul / Laravel Cookie Consent Scripts -->
    {!! CookieConsent::scripts() !!}
</body>
</html>
This Master layout ensures that any page extending it will automatically include the navbar, footer, and other global elements including the DevRabiul Laravel Cookie Consent styles and scripts. With this setup, the cookie consent banner will be available on all pages that use this Master layout, keeping your templates consistent and maintainable.

Step # 6 : Create a Home View.

Next, create a Home page that extends the Master layout. This page will use the global Master layout we just created. Create a new file at: resources/views/home.blade.php. Add the following content.
@extends('layouts.master')
@section('content')
    <section class="text-center py-16">
        <h1 class="text-4xl font-bold text-white mb-4">Welcome to Code Shotcut</h1>
        <p class="text-gray-400 mb-6">
            Learn practical Laravel tutorials step by step.
        </p>
        <a href="https://codeshotcut.com" target="_blank" class="inline-block px-6 py-3 bg-blue-600 text-white rounded hover:bg-blue-700 transition">
            Visit Code Shotcut
        </a>
    </section>
    <section class="max-w-4xl mx-auto px-4 py-8 text-gray-200">
        <h2 class="text-2xl font-semibold mb-4">Code Shotcut</h2>
        <p class="mb-4">
            Code Shotcut is a YouTube channel and website dedicated to practical Laravel tutorials and real-world coding tips.
        </p>
        <p>
            Follow along to learn step-by-step implementations, tips, and tricks to improve your Laravel skills.
        </p>
    </section>
@endsection
This Home page now correctly extends the Master layout, automatically including the navbar, footer, and cookie consent functionality.

Step # 7 : Update the Route.

Now, let’s update the route so that when you visit root URL (/) in your browser, it shows the Home page we just created. Open the routes/web.php file and modify the route like this.
Route::get('/', function () {
    return view('home');
});
This update makes your Home page load at the root URL (/).

Step # 8 : Test the DevRabiul Cookie Consent Package.

Start the Laravel development server.
php artisan serve
Visit: http://127.0.0.1:8000. You should see the cookie consent banner on the Home page. This confirms that the DevRabiul Laravel Cookie Consent package is working and the banner appears correctly on pages using the Master layout.


Step # 9 : Customize the Cookie Consent Banner.

To change the text, colors, or theme of the cookie consent banner, open the configuration file: config/cookie-consent.php. Set the banner theme using the theme_preset option.
'theme_preset' => env('COOKIE_CONSENT_THEME_PRESET', 'modern-blue'),
You will see the banner in a modern-blue theme. You can also configure cookie categories and control whether they are locked or optional.
'cookie_categories' => [
    'necessary' => [
        'enabled' => true,
        'locked' => false, // Locked by default; set to false if you want to allow customization
        'title' => 'Essential Cookies',
        'description' => 'These cookies are essential for the website to function properly.',
    ],
    // Add other categories here (analytics, marketing, etc.)
],


Other options you can configure include:
  1. Message: The text displayed in the banner.
  2. Accept_button_text:Text for the accept button.
  3. Decline_button_text:Text for the decline button.
  4. Settings_button_text: Text for the settings/preferences button.
  5. Policy_url: Link to your privacy policy.

Any changes here will automatically reflect on all pages using the Master layout.

Conclusion

By following this guide, you’ve successfully added a GDPR compliant cookie consent banner to your Laravel 12 application using the DevRabiul Laravel Cookie Consent package. This setup provides a simple and effective way to manage cookie categories, display a customizable banner, and ensure compliance with privacy regulations across all pages using the Master layout. The configuration is easy to adjust, allowing you to change themes, text, buttons, and links to fit your application’s needs.
For more details, check out the official DevRabiul 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