dek-cz/meta-canonical-control

Nette control for managing meta data and canonical link in HTML header.

1.6.2 2022-05-03 10:06 UTC

This package is auto-updated.

Last update: 2024-10-30 01:32:00 UTC


README

Build Status Downloads this Month Latest stable Coverage Status

Installation

Via Composer:

$ composer require dek-cz/meta-canonical-control

Usage

First register the control factory in your config and optionally set up default metadata:

services:
    -
        implement: Dekcz\MetaControl\IMetaControlFactory
        setup:
            - setCharset('utf-8')
            - setAuthor('Jon Doe')

Use the control factory in your presenter:

protected function createComponentMeta(): Dekcz\MetaControl\MetaControl
{
    $control = $this->metaControlFactory->create();
    $control->setDescription('Lorem ipsum');
    return $control;
}

And render it in your Latte template:

<html>
<head>
    {control meta}
</head>
<body>
    ...
</body>
</html>

Overview of supported meta tags

Charset:

// <meta charset="utf-8">
$control->setCharset('utf-8');
$control->getCharset(); // 'utf-8'

Document metadata:

// <meta name="author" content="John Doe">
$control->setMetadata('author', 'Jon Doe');
$control->getMetadata('author'); // 'Jon Doe'

Document properties:

// <meta property="og:title" content="Foo title">
$control->setProperty('og:title', 'Foo title');
$control->getProperty('og:title'); // 'Foo title'

Pragma directives:

// <meta http-equiv="content-type" content="text/html; charset=UTF-8">
$control->setPragma('content-type', 'text/html; charset=UTF-8');
$control->getPragma('content-type'); // 'text/html; charset=UTF-8'

Shorthands for standard metadata

Author:

// <meta name="author" content="John Doe">
$control->setAuthor('Jon Doe');
$control->getAuthor(); // 'Jon Doe'

Description:

// <meta name="description" content="Lorem ipsum">
$control->setDescription('Lorem ipsum');
$control->getDescription(); // 'Lorem ipsum'

Keywords:

// <meta name="keywords" content="foo, bar, baz">
$control->setKeywords('foo', 'bar');
$control->addKeyword('baz');
$control->getKeywords(); // ['foo', 'bar', 'baz']

Robots:

// <meta name="robots" content="noindex, nofollow">
$control->setRobots('noindex, nofollow');
$control->getRobots(); // 'noindex, nofollow'

New features

Canonical link:

// <link rel="canonical" href="/test/3">
$control->setCanonical('/test/3');

Prev (paging):

// <link rel="prev" href="test/3/page/1">
$control->setPrev('/test/3/page/1');

Next (paging):

// <link rel="next" href="/test/3/page/3">
$control->setNext('/test/3/page/3');

Set not unique meta e.g. google-site-verification

// <meta name="google-site-verification" content="123456789abcdefghijklmnopqrstuvwxyzABCDEFGH">\n<meta name="google-site-verification" content="HGFEDCBAzyxwvutsrqponmlkjihgfedcba987654321">\n

$control->setMetadata('google-site-verification', '123456789abcdefghijklmnopqrstuvwxyzABCDEFGH');
$control->setMetadata('google-site-verification', 'HGFEDCBAzyxwvutsrqponmlkjihgfedcba987654321');