Laravel 11 - Statikbe Cookie Consent For EU
Laravel 11 - Statikbe Cookie Consent For EU
In this tutorial, we will explore how to implement Statikbe Cookie Consent for the EU in Laravel 11, ensuring compliance and enhancing user trust.
If you're a video person, feel free to skip the post and check out the video instead!
Quick Overview
This guide walks through integrating the Laravel Cookie Consent package into a Laravel project. It starts with creating a new Laravel project or using an existing one, followed by installing the Cookie Consent package via Composer. Next, the required JavaScript and CSS files are published to ensure proper styling of the consent modal. A master Blade template is set up to display the cookie consent banner across all pages. The guide then covers updating routes, registering middleware in AppServiceProvider.php, and including the consent stylesheet. After setting up, the Laravel development server is started to test the functionality. Finally, customization options are explored, including modifying consent messages via localization files and configuring cookie settings in config/cookie-consent.php.
Step # 1 : Create fresh Laravel project or use existing project.
To create a new Laravel project, you can use the following command if you have Laravel installed globally.
laravel new cookie
Or
Alternatively, you can create the project using Composer.
composer create-project laravel/laravel --prefer-dist cookie
During the setup process, select the following options when prompted.
- Would you like to install the starter kit? — Select None
- Testing framework — Select Pest
- Database for your application — Select MySQL
- Run the default database migration — Type Yes
This step sets up a new Laravel project named cookie using either the Laravel CLI or Composer. Choosing None for the starter kit ensures a minimal setup, while Pest is selected as the testing framework. MySQL is set as the database, and running migrations prepares the schema for development.
Step # 2 : Access the project.
Open a terminal (e.g., Git Bash) and navigate to your Laravel project's root folder.
cd c:xampp/htdocs/cookie
This step ensures you're in the correct project folder, allowing you to run Laravel commands, configure settings, and start development.
Step # 3 : Install the Laravel Cookie Consent Package.
Run the following command to install the package.
composer require statikbe/laravel-cookie-consent
Once installed, the package registers itself automatically, eliminating the need for additional setup. It provides a ready-to-use cookie consent banner that helps websites comply with cookie regulations.
Step # 4 : Publish JavaScript and CSS Files.
Run the following command to publish the required assets.php artisan vendor:publish --provider="Statikbe\CookieConsent\CookieConsentServiceProvider" --tag="cookie-public"
This command publishes the JavaScript and CSS files needed for the cookie consent banner. These assets ensure that the banner is properly styled and functions correctly on the frontend.
Step # 5 : Create a Master View.
In this tutorial, I will be using a sample Master Blade template that includes the basic HTML structure. This template ensures that the cookie consent modal appears consistently across all pages. It also includes Tailwind CSS for styling and provides a simple layout to demonstrate cookie consent integration.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Code Shotcut - Cookie Consent Modal for EU</title>
<link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
</head>
<body class="bg-gray-800">
<div class="container mx-auto mt-10">
<div class="text-center">
<h1 class="text-4xl font-bold text-white mb-4">Welcome to Code Shotcut!</h1>
<p class="text-lg text-gray-400 mb-8">This is a sample website demonstrating cookie consent.</p>
<p class="text-md text-gray-300 mb-4">
You can include the cookie consent functionality by adding the middleware to your web routes and publishing the package assets.
Ensure to follow the instructions in the <strong>Laravel Cookie Consent</strong> documentation for proper integration.
</p>
<p class="text-md text-gray-300 mb-4">
It is recommended to include the necessary CSS and JS files in your master or base layout Blade file.
This ensures that the cookie consent modal is available on all pages of your application.
</p>
<p class="text-md text-gray-300">
For more details, check the <a href="https://github.com/statikbe/laravel-cookie-consent" class="text-blue-400 underline" target="_blank">official documentation</a>.
</p>
</div>
</div>
</body>
</html>
This master template serves as the foundation for your Laravel application. By including the necessary CSS and JavaScript files here, the cookie consent modal will automatically appear when the package is properly configured.
Step # 6 : Update the Route.
Update the route to ensure that the homepage loads the master view.
Route::get('/', function () {
return view('master');
});
This ensures that when users visit the homepage, the master Blade template is rendered, allowing the cookie consent functionality to be displayed properly.
Step # 7 : Include Cookie Consent CSS and Register Middleware.
Add the cookie consent stylesheet to the master view to ensure proper styling.
<link rel="stylesheet" type="text/css" href='{{asset("vendor/cookie-consent/css/cookie-consent.css")}}'>
To enable the cookie consent functionality globally, update the boot() method in app/Providers/AppServiceProvider.php to register the CookieConsentMiddleware within the web middleware group, as shown below.
<?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 ensures that the cookie consent functionality is applied to all web routes automatically.
Step # 8 : It's time to test.
Start the Laravel development server by running the following command.
php artisan serve
Once the server is running, open your browser and visit.
127.0.0.1:8000
Step # 9 : Customizing Cookie Consent.
Modifying Cookie Consent Messages :
To customize the cookie consent dialog messages, run the following command.
php artisan vendor:publish --provider="Statikbe\CookieConsent\CookieConsentServiceProvider" --tag="cookie-lang"
This will generate localization files inside lang/vendor/cookie-consent, allowing modifications in different languages.
To update the default messages, open the respective file in lang/vendor/cookie-consent/en and modify the accept button text.
From :
'alert_accept' => 'Accept all cookies',
To :
'alert_accept' => 'Custom Message - Accept all cookies',
This lets you personalize the consent dialog to match your application’s branding.
Configuration of Cookie Consent :
To publish the configuration file, run.
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' => [],
'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 settings allow you to configure how cookies are stored, their expiration time, and the URLs for your privacy policies in different languages.
Conclusion
By following this guide, you've successfully integrated the Laravel Cookie Consent package into your application. Your website now includes a functional cookie consent banner that ensures compliance with cookie regulations. The package simplifies the process by providing built-in middleware, customizable messages, and configuration options. You can further enhance the implementation by modifying the consent messages, adjusting cookie settings, and styling the banner to match your brand.
For more details, refer to the official Laravel Cookie Consent documentation.
Share this with friends!
To engage in commentary, kindly proceed by logging in or registering
Subscribe to Our Newsletter
Stay ahead of the curve! Join our newsletter to see what everyone’s talking about.
0 Comments