涅槃-msu / yii2-jsonld-helper
此包已被弃用且不再维护。未建议替代包。
Yii2 的 JsonLD 辅助类
dev-master / 1.0.x-dev
2019-02-10 22:20 UTC
Requires
- ml/json-ld: 1.*
- yiisoft/yii2: *
This package is not auto-updated.
Last update: 2021-11-27 03:52:30 UTC
README
为 Yii2 提供注册 JSON-LD 格式结构化数据标记的辅助类。
资源
安装
Composer
将扩展添加到您的 composer.json
并按常规更新依赖项,例如运行 composer update
{ "require": { "nirvana-msu/yii2-jsonld-helper": "1.0.*@dev" } }
## 示例用法
为了让搜索引擎知道如何在搜索结果中显示您的网站名称,您可以在您的着陆页上添加以下 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 许可下发布。