PHP code example of elegantly / laravel-cookies-consent
1. Go to this page and download the library: Download elegantly/laravel-cookies-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/ */
elegantly / laravel-cookies-consent example snippets
return [
/*
|--------------------------------------------------------------------------
| URL Configuration
|--------------------------------------------------------------------------
|
| These values determine the package's API route URLs. Both values are
| nullable and represent the same concepts as Laravel's routing parameters.
|
*/
'url' => [
'domain' => null,
'prefix' => 'cookiesconsent',
],
/*
|--------------------------------------------------------------------------
| Consent Cookie Configuration
|--------------------------------------------------------------------------
|
| To keep track of the user's preferences, this package stores
| an anonymized cookie. You do not need to register this cookie in the
| package's cookie manager as it is done automatically (under "essentials").
|
| The duration parameter represents the cookie's lifetime in minutes.
|
| The domain parameter, when defined, determines the cookie's activity domain.
| For multiple sub-domains, prefix your domain with "." (e.g., ".mydomain.com").
|
*/
'cookie' => [
'name' => Str::slug(env('APP_NAME', 'laravel'), '_').'_cookiesconsent',
'lifetime' => 60 * 24 * 365,
'domain' => null,
],
/*
|--------------------------------------------------------------------------
| Legal Page Configuration
|--------------------------------------------------------------------------
|
| Most cookie notices display a link to a dedicated page explaining
| the extended cookies usage policy. If your application has such a page,
| you can add its route name here.
|
*/
'policy' => null,
];
namespace App\Http\Middleware;
use Carbon\CarbonInterval;
use Closure;
use Elegantly\CookiesConsent\CookieDefinition;
use Elegantly\CookiesConsent\CookieGroupDefinition;
use Elegantly\CookiesConsent\Facades\CookiesConsent;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
class RegisterCookiesConsent
{
public function handle(Request $request, Closure $next): Response
{
// Register cookies related to the Facebook pixel
CookiesConsent::register(new CookieGroupDefinition(
key: 'marketing',
name: __('cookies-consent::cookies.marketing.name'),
description: __('cookies-consent::cookies.marketing.description'),
items: [
new CookieDefinition(
name: '_fbc',
lifetime: CarbonInterval::years(2),
description: __('cookies-consent::cookies._fbc.description')
),
new CookieDefinition(
name: '_fbp',
lifetime: CarbonInterval::years(3),
description: __('cookies-consent::cookies._fbp.description')
),
],
onAccepted: function () {
return <<<'JS'
if (typeof fbq === 'function') {
fbq('consent', 'grant');
}
JS;
},
));
return $next($request);
}
}
use Elegantly\CookiesConsent\Facades\CookiesConsent;
CookiesConsent::registerEssentials()
->register(
// ... custom cookie definition
)
use Elegantly\CookiesConsent\Facades\CookiesConsent;
CookiesConsent::register(new CookieGroupDefinition(
// ...
onAccepted: function () {
return <<<'JS'
// This JavaScript code will be executed when consent is granted
if (typeof fbq === 'function') {
fbq('consent', 'grant');
}
JS;
},
));
use Elegantly\CookiesConsent\Facades\CookiesConsent;
CookiesConsent::getDefinition();
use Elegantly\CookiesConsent\Facades\CookiesConsent;
use Elegantly\CookiesConsent\CookieGroupDefinition;
use Elegantly\CookiesConsent\CookieDefinition;
use Carbon\CarbonInterval;
CookiesConsent::register(new CookieGroupDefinition(
key: 'marketing', // customize this value if you want
name: __('cookies-consent::cookies.marketing.name'), // customize this value if you want
description: __('cookies-consent::cookies.marketing.description'), // customize this value if you want
items: [
new CookieDefinition(
name: '_fbc',
lifetime: CarbonInterval::years(2),
description: __('cookies-consent::cookies._fbc.description')
),
new CookieDefinition(
name: '_fbp',
lifetime: CarbonInterval::years(3),
description: __('cookies-consent::cookies._fbp.description')
),
],
onAccepted: function () {
return <<<'JS'
if(typeof fbq === 'function'){
fbq('consent', 'grant');
}
JS;
},
));