heimrichhannot / contao-head-bundle
此模块包含对 contao 前端页面 <head> 区域的增强。
Requires
- php: ^7.4 || ^8.0
- contao/core-bundle: ^4.9
- heimrichhannot/contao-utils-bundle: ^2.219
- psr/container: ^1.0 || ^2.0
- spatie/schema-org: ^3.4
- symfony/config: ^4.4 || ^5.4
- symfony/dependency-injection: ^4.4 || ^5.4
- symfony/http-kernel: ^4.4 || ^5.4
- symfony/polyfill-php80: ^1.26
- symfony/service-contracts: ^1.0 || ^2.0 || ^3.0
- dev-master
- 1.13.4
- 1.13.3
- 1.13.2
- 1.13.1
- 1.13.0
- 1.12.1
- 1.12.0
- 1.11.1
- 1.11.0
- 1.10.2
- 1.10.1
- 1.10.0
- 1.9.1
- 1.9.0
- 1.8.0
- 1.7.0
- 1.6.0
- 1.5.2
- 1.5.1
- 1.5.0
- 1.4.3
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.1
- 1.3.0
- 1.2.5
- 1.2.4
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.9
- 1.1.8
- 1.1.7
- 1.1.6
- 1.1.5
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.10
- 1.0.9
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- dev-contao5
This package is auto-updated.
Last update: 2024-08-28 09:55:43 UTC
README
此捆绑包增强了处理 html <head>
部分标签的处理。它提供从您的代码中动态更新 head 标签的服务。
功能
- 提供设置 head 标签(如 meta、title、base、link)的便捷 API
- 提供额外的 json-ld 模式数据
- 默认设置重要的 meta 标签,如 og:title、og:description、og:url 和 twitter:card
- 允许在首页上设置 open graph 和 twitter 备用图像
- 允许为每个首页设置twitter作者
- 从 contao 4.13 向 contao 4.9+ 返回 canonical url 选项
- 从 contao 4.9+ 返回 json-ld 支持
用法
设置
-
使用 composer 安装
-
更新您的数据库
-
设置以下配置变量(如果您不需要旧版实现)
huh_head: use_contao_head: true use_contao_variables: true
-
可选:在首页上设置备用图像和twitter作者
添加额外的 meta 标签
在您的首页上,您可以激活添加备用图像(og:image 和 twitter:image)和twitter用户名(twitter:site)meta 标签到您的网页。
添加额外的 schema.org 数据
在您的首页上,您可以激活添加额外的结构化数据到您的网页。以下 schema.org 类型可用
- @Organization
- @WebSite
- @WebPage
- @BreadcrumbList
在您的模板中设置 json-ld
此捆绑包将 contao 4.12+ 的方法返回到 contao 4.9+。因此用法与 contao 核心相同。
Twig 模板
{% do add_schema_org({ '@type': 'NewsArticle', 'headline': newsHeadline|striptags, 'datePublished': datetime|date('Y-m-d\TH:i:sP'), }) %}
PHP 模板
<?php $this->addSchemaOrg([ '@type' => 'NewsArticle', 'headline' => $newsHeadline, 'datePublished' => $datetime->date('Y-m-d\TH:i:sP'), ]); ?>
集成
在您的代码中使用设置的 head 捆绑包 API。
设置 head 内容
要设置 base、title、meta 和 link 标签,请使用 HtmlHeadTagManager
服务
use HeimrichHannot\HeadBundle\HeadTag\BaseTag; use HeimrichHannot\HeadBundle\HeadTag\MetaTag; use HeimrichHannot\HeadBundle\HeadTag\TitleTag; use HeimrichHannot\HeadBundle\HeadTag\Meta\CharsetMetaTag; use HeimrichHannot\HeadBundle\HeadTag\Meta\HttpEquivMetaTag; use HeimrichHannot\HeadBundle\HeadTag\Meta\PropertyMetaTag; use HeimrichHannot\HeadBundle\Manager\HtmlHeadTagManager; use Symfony\Component\HttpFoundation\Request; class SomeEventListener { private HtmlHeadTagManager $headTagManager; public function updateBaseTag(Request $request): void { // Set base tag to null to remove it $this->headTagManager->setBaseTag(null); //Set base tag from object or url $this->headTagManager->setBaseTag(new BaseTag($request->getSchemeAndHttpHost())); $this->headTagManager->setBaseTag('https://example.org')); } public function updatedTitleTag(): void { // Set title to "Hello World" $this->headTagManager->setTitleTag('Hello World'); // Set title tag from object and adjust output format $this->headTagManager->setTitleTag(new TitleTag('Foo Bar', '%s | {{page::rootPageTitle}}')) // Will output: <title>Foo Bar | My Great Website Page Title</title> } public function setMetaTags(): void { // Add a new meta tag. If a tag with the same name already exists, it will be overridden $this->headTagManager->addMetaTag(new MetaTag('author', 'John Doe')); // Get an existing tag $description = ($tag = $this->headTagManager->getMetaTag('og:description')) ? $tag->getContent() : ''; // Remove a tag $this->headTagManager->removeMetaTag('twitter:site'); // Create a tag for property meta tags $this->headTagManager->addMetaTag(new PropertyMetaTag('og:type', 'article')); // Create a http-equiv tag $this->headTagManager->addMetaTag(new HttpEquivMetaTag('refresh', '30')); // Set a charset tag $this->headTagManager->addMetaTag(new CharsetMetaTag('UTF-8')); // Create tags without class (usefull when creating tags in a loop without custom checks) $this->headTagManager->addMetaTag( $this->headTagManager->getHeadTagFactory()->createMetaTag('description', 'Lorem ipsum!') ); $this->headTagManager->addMetaTag( $this->headTagManager->getHeadTagFactory()->createTagByName('meta_og:url', 'https://example.org') ); } public function setLinkTags(): void { // Add a new link tag. If a tag with the same name already exists, it will be overridden $this->headTagManager->addLinkTag(new LinkTag('next', 'https://example.org?page=2')); // Get an existing tag $this->headTagManager->getLinkTag('prev'); // Remove a tag $this->headTagManager->removeLinkTag('prev'); // Shorthand for canonical tag $this->headTagManager->setCanonical('https://example.org'); } }
设置 json-ld 模式数据
从 contao 4.12 开始,您可以使用来自核心的 JsonLdManager 服务 JsonLdManager。
要设置 json-ld 模式数据,请使用 JsonLdManager
服务
<?php use HeimrichHannot\HeadBundle\Manager\JsonLdManager; class ExampleController { private JsonLdManager $jsonLdManager; public function __invoke() { $organisation = $this->jsonLdManager->getGraphForSchema(JsonLdManager::SCHEMA_ORG)->organization(); $organisation->name('Example and Sons Ltd.'); $organisation->url('https://example.org'); } }
Reader Config Contao 示例
旧版集成
确保,huh_head.use_contao_head
和/或 huh_head.use_contao_variables
没有设置为 true。
在 fe_page 模板 () 中输出 $this->meta()
<?php $this->block('meta'); ?>
<?= $this->meta(); ?>
<?php $this->endblock(); ?>
确保,您已删除(如果 huh_head.use_contao_head
不是 true,则由 $this->meta()
输出)
<meta charset="<?= $this->charset ?>">
<title><?= $this->title ?></title>
<base href="<?php echo $this->base; ?>">
<meta name="robots" content="<?= $this->robots ?>">
<meta name="description" content="<?= $this->description ?>">
该 meta
函数目前接受一个参数,该参数可以包含应跳过的服务名称(数组)。
开发者
后端字段
获取选择字段的标签选项。如果您想自定义选项,请将 meta 标签选项的前缀为 meta_
。
use HeimrichHannot\HeadBundle\Helper\DcaHelper; class HeadTagOptionsListener { private DcaHelper $dcaHelper; public function __invoke() { return $this->dcaHelper->getTagOptions([ // filter: (array|null) If set, only tags fulfill given filters will be returned. See FILTER constants for available options. Default null 'filter' => [DcaHelper::FILTER_META, DcaHelper::FILTER_TITLE], // skip_tags: (array) Skip specific tags. Default empty 'skip_tag' => ['og:locale'], ]); } }
如何评估字段值的示例
use Contao\ContentModel; use HeimrichHannot\HeadBundle\Manager\HtmlHeadTagManager; class SomeEventListener { private HtmlHeadTagManager $headTagManager; public function __invoke(ContentModel $contentModel){ $tag = $this->headTagManager->getHeadTagFactory()->createTagByName($contentModel->headTag); if ($tag) { $tag->setAttribute("content", $contentModel->headTagContent); $this->headTagManager->addTag($tag); } } }
配置参考
# Default configuration for extension with alias: "huh_head" huh_head: # Use the default head variables for title,base,robots and description instead of removing them from the page template. use_contao_head: false # Use the default contao template variables for outputting head tags instead of the meta function. use_contao_variables: false