powerkernel / yii2-jsonld-helper
为 Yii2 提供的 JsonLD 助手类
0.1.0
2018-04-20 10:30 UTC
Requires
This package is auto-updated.
Last update: 2024-09-11 01:16:23 UTC
README
Yii2 助手类,用于在 JSON-LD 格式中注册结构化数据标记。
资源
安装
Composer
将扩展添加到您的 composer.json
文件中,并像通常一样更新您的依赖项,例如运行 composer update
{ "require": { "powerkernel/yii2-jsonld-helper": "*" } }
## 示例用法
为了让搜索引擎知道如何在搜索结果中显示您的网站名称,您可以在着陆页上的某个位置添加以下 JSON-LD 文档
$doc = (object)[ "@type" => "http://schema.org/WebSite", "http://schema.org/name" => Yii::$app->params['brand'], "http://schema.org/url" => Yii::$app->urlManager->hostInfo ]; JsonLDHelper::add($doc);
如果需要使用除默认的 ["@vocab" => "http://schema.org/"]
之外的值,可以将 $context
作为可选的第二个参数传递
JsonLDHelper::add($doc, $context);
请注意,这样做可能会导致生成的脚本无法通过谷歌 [SDTT] (https://search.google.com/structured-data/testing-tool) 的验证 - 详细信息请参阅此 stackoverflow 问题
您还可以使用 JsonLDHelper::addBreadcrumbList
添加基于应用程序视图 breadcrumbs
参数的 BreadcrumbList
schema.org 标记。例如,在布局的起始位置添加
JsonLDHelper::addBreadcrumbList();
最后,必须在布局的 <head>
部分中调用 JsonLDHelper::registerScripts
方法,例如
<head> <!-- ... --> <?php JsonLDHelper::registerScripts(); ?> <?php $this->head() ?> </head>
### 嵌套数据的示例
$doc = [ "@type" => "http://schema.org/BlogPosting", "http://schema.org/mainEntityOfPage" => (object)[ "@type" => "http://schema.org/WebPage", "@id" => "http://example.com/awesome-blog-post", ], "http://schema.org/headline" => "Post Title", "http://schema.org/articleBody" => "Post Body", "http://schema.org/author" => (object)[ "@type" => "http://schema.org/Person", "http://schema.org/name" => "Jon Snow", "http://schema.org/url" => "http://example.com", "http://schema.org/sameAs" => [ "https://www.instagram.com/kitharingtonn/", ] ], ]; JsonLDHelper::add($doc);
请注意,此扩展只是围绕 lanthaler/JsonLD 处理器的薄包装 - 请参阅此库的完整文档。
## 许可证
扩展在 MIT 许可证下发布。