contenir / contenir-consent-laminas-mvc
Laminas MVC adapter for contenir/contenir-consent: consent()->head() and consent()->bodyEnd() write the cookie banner and Consent Mode, consentScript() gates a site's own embeds, consentPreferencesLink() reopens the preferences, and a route serves the banner's files.
Package info
github.com/contenir/contenir-consent-laminas-mvc
pkg:composer/contenir/contenir-consent-laminas-mvc
Requires
- php: ~8.3.0 || ~8.4.0 || ~8.5.0
- contenir/contenir-consent: ^0.1
- laminas/laminas-http: ^2.19
- laminas/laminas-router: ^3.13
- laminas/laminas-servicemanager: ^3.22 || ^4.0
- laminas/laminas-stdlib: ^3.19
- laminas/laminas-view: ^2.30
- psr/container: ^1.1 || ^2.0
Requires (Dev)
- contenir/contenir-qa-tools: 0.1.x-dev
- infection/infection: ^0.34.1
- phpunit/phpunit: ^11.5.42
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-10-21 03:53:14 UTC
README
The Laminas MVC adapter for contenir/contenir-consent. It puts the cookie banner on a site's pages, gates the site's own embeds until visitors allow them, and serves the banner's files.
Consent is set in the CMS, by the Consent add-on. Settings are read
from the site's config['settings'], as the CMS writes them, so there is nothing to configure on the site.
Requirements
- PHP 8.3, 8.4 or 8.5
- laminas/laminas-view 2.30 or later, and laminas/laminas-router 3.13 or later
Install
composer require contenir/contenir-consent-laminas-mvc
Add Contenir\Consent\Laminas\Mvc to the application's modules (laminas-component-installer offers to).
The package is named after its repository, as every Contenir package is. Before 0.1.0 it was
contenir/consent-laminas-mvc.
Usage
In the layout, put consent()->head() in <head>, before any tag, and consent()->bodyEnd() straight before
</body>. On a site with the Tracking add-on, tracking()->head() can come before or after consent()->head():
<head>
<meta charset="utf-8">
<?= $this->consent()->head($nonce) ?>
<?= $this->tracking()->head($nonce) ?>
…
</head>
<body>
…
<footer>
<?= $this->consentPreferencesLink(['class' => 'footer-link']) ?>
</footer>
<?= $this->consent()->bodyEnd($nonce) ?>
</body>
| Helper | Writes |
|---|---|
consent()->head() |
the banner's stylesheet, and on a site without the Tracking add-on, Google Consent Mode's denied defaults |
consent()->bodyEnd() |
the banner's script and the call that starts it for this visitor |
consentScript($category, src: …) or consentScript($category, code: …) |
one of the site's own scripts, inert until the visitor allows $category |
consentPreferencesLink($attributes, $label) |
a link that opens the cookie preferences |
Gate a site's own embeds, such as videos, maps and chat widgets, with consentScript():
<?= $this->consentScript('preferences', src: 'https://www.youtube.com/iframe_api') ?> <?= $this->consentScript('marketing', code: 'initChat();', attributes: ['id' => 'chat']) ?>
The category is necessary, analytics, marketing or preferences. List the embed's service in the config (below)
so the preferences say what it is.
Every script carries the page's nonce: the one passed to the helper, or else the one from a
Contenir\Consent\Laminas\Mvc\NonceProviderInterface service the application registers. One class can implement this
and contenir/tracking-laminas-mvc's interface of the same name.
What it writes, and when
- Nothing until the Consent add-on is enabled on the site. Until then
consentScript()writes each script to run at once, andconsentPreferencesLink()writes nothing, since there is no banner to open. - The enabled add-ons are read from
config/autoload/extensions.global.phpandextensions.local.php, as the CMS reads them: a list in the local file replaces the global one entirely. - The visitor's country comes from the request header the settings name, Cloudflare's
CF-IPCountryby default, for the "ask only the EEA, UK and Switzerland" mode. Pages cached by a proxy must vary on that header in that mode. - The banner's files are served by the module at
/_consent/consent-<hash>.jsand.css, with a route ahead of the site's own (priority 1000), cacheable for a year. A change to either file changes its name.
The banner, its categories, the contenir:consent browser event and Google Consent Mode are described in
contenir/contenir-consent.
Configuration
Nothing is needed. To add to the defaults:
return [ 'contenir_consent' => [ 'extensions_config' => 'config/autoload', // where extensions.global.php and .local.php are 'services' => [ // the site's own services, listed in the preferences 'youtube' => [ 'name' => 'YouTube', 'provider' => 'Google', 'category' => 'preferences', 'cookies' => ['YSC', 'VISITOR_INFO1_LIVE'], 'privacy_url' => 'https://policies.google.com/privacy', ], ], 'texts' => [ // replaces contenir/contenir-consent's Texts defaults, by name 'acceptAll' => 'Accept all cookies', ], ], ];
To restyle the banner, set cookieconsent's --cc-* CSS variables on #cc-main in the site's stylesheet.
Licence
BSD-3-Clause. See LICENSE.md.