Search by

coercive / head

Coercive

Coercive Utility Head

0.1.0 2026-10-20 07:13 UTC

This package is auto-updated.

Last update: 2026-10-20 07:17:01 UTC


README

Handler for html head tags

Get

composer require coercive/head

Compatible PHP 7.4 → 8.5

Tests

composer install
vendor/bin/phpunit

PHPUnit 12 requires PHP 8.3+ to run the tests ; the library itself still runs on PHP 7.4.

Example

<?php
use Coercive\Utility\Head\Head;
use Coercive\Utility\Head\Link;
use Coercive\Utility\Head\Script;

$head = new Head;

# Charset (always rendered first) : <meta charset="utf-8" />
$head->Charset();

# Title
$head->Title()->setContent('Site name');
$head->Title()->addContentBefore('Page name'); # Page name | Site name

# Meta Description
$head->Description()->setContent('example description');

# Any meta by name
$head->Meta('DC.title')->setContent('example title');

# Several metas with the same name
$head->addMeta('DC.creator', 'First author', false);
$head->addMeta('DC.creator', 'Second author', false);

# Refresh : <meta http-equiv="refresh" content="5;URL=/home" />
$head->setRefresh(5, '/home');

# Links and scripts
$head->addCanonical('https://example.com/page');
$head->addCssLink('/css/style.css');
$head->addScriptSource('/js/app.js');
$head->addLink((new Link)->setRel('schema.DC')->setHref('http://purl.org/dc/elements/1.1/'));
$head->addIcon('/favicon.svg', 'image/svg+xml');
$head->addPreconnect('https://fonts.gstatic.com', true);
$head->addPreload('/fonts/font.woff2', 'font', 'font/woff2', true);
$head->addAlternate('https://example.com/en/page', '', 'en');

# Content-Security-Policy nonce
$head->addCustomScript((new Script)->setNonce($nonce)->setContent('console.log(1);'));

# Open Graph / Twitter
$head->Og()->setTitle('example title')->setImage('https://example.com/image.jpg');
$head->Og()
    ->setType('article')
    ->setArticlePublishedTime('2026-10-20T08:00:00+02:00')
    ->addArticleAuthor('First author')
    ->addArticleTag('php');
$head->Twitter()->setCard('summary')->setTitle('example title')->setImageAlt('image description');

# Custom values can be stored : they are not rendered
$head->set('GOOGLE_ANALYTICS', 'G-XXXX');

# Show
echo $head->toHtml();

Double quotes are removed from the attribute values and from the title.