portrino / px_semantic
TYPO3的Structured Data渲染。基于Hydra轻松构建Linked Data API。
Requires
- php: >= 5.5.0, <= 7.1.99
- typo3/cms-core: >= 6.2.16, <= 8.7.99
Suggests
- api-platform/schema-generator: ~1.2.0
Replaces
- typo3-ter/px_semantic: 2.5.1
README
基于JSON-LD的TYPO3结构化数据标记渲染
PxSemantic提供了一种极为动态且高度可定制的解决方案,用于在您的网站上嵌入结构化数据,并通过有关您网站的信息来丰富它们。当前支持的类是https://schema.org/上定义的词汇表的一个子集,并且是由https://api-platform.com/的schema生成器生成的。目前,我们只支持json-ld格式来编码LinkedData。
开始之前
示例1 - 渲染组织信息
例如,您想使用关于您的企业联系人的信息(https://developers.google.com/structured-data/customize/contact-points)标记您的官方网站,您可以这样做
通过TypoScript声明您的组织信息和企业联系人。
TypoScript
lib.structuredDataMarkupOrganization < lib.structuredDataMarkup
lib.structuredDataMarkupOrganization {
settings {
entity = Portrino\PxSemantic\SchemaOrg\Organization
processors {
# processor to set properties of an entity via TypoScript
0 {
className = Portrino\PxSemantic\Processor\TypoScriptProcessor
settings {
# for string values you do not have to use TEXT datatype, you can just write "key = value"
# US, GB, DE
areaServed = DE
contactPoint {
# "customer support", "technical support", "billing support", ...
contactType = customer support
telephone = +49-123-456-78
}
logo {
# get the logo image uri via TS and prepend the baseUrl to get the absolute path
url = IMG_RESOURCE
url {
file = fileadmin/company_logo.png
stdWrap.wrap = {TSFE:baseUrl}|
stdWrap.insertData = 1
}
}
# special datatype from px_semantic EXT to define arrays
sameAs = Portrino\PxSemantic\Converter\ArrayConverter
sameAs {
0 = https://#/yourSite/
1 = http://www.youtube.com/user/yourSite/
2 = http://www.pinterest.com/yourSite/
}
# use typolink to generate link to the root page
url = TEXT
url.typolink {
parameter = 1
returnLast = url
}
}
}
}
}
}
使用TS在您的网站上某个位置渲染生成的JSON-LD代码。我们将其放入<HEAD>
中,因为其他元信息也位于<HEAD>
标签内。但这并不重要。
谷歌说
您可以使用schema.org字段名称和新的JSON-LD数据格式在网页中嵌入数据。JSON-LD可以通过许多广泛可用的JSON编码器轻松生成。以下示例中所示的数据,在
或区域。无论哪种方式,都不会影响您的文档在用户网络浏览器中的显示。-- [谷歌文档][1]<script type="application/ld+json"> ... </script>
标签内封闭,可以放置在显示该事件的页面的
TypoScript
page.headerData.1453734422 < lib.structuredDataMarkupOrganization
如果您想在模板中放置它,可以使用fluid cObject
-ViewHelper
Fluid
<f:cObject typoscriptObjectPath="lib.structuredDataMarkupOrganization"></f:cObject>
这将导致您的页面出现以下JSON-LD标记
<script type="application/ld+json"> { "@context" : "http://schema.org", "@type" : "Organization", "areaServed":"DE" "url" : "http://www.your-company-site.com", "contactPoint" : { "@type" : "ContactPoint", "areaServed":"DE", "telephone" : "+49-123-456-78", "contactType" : "customer support" }, "logo": { "@type":"ImageObject", "url":"http://www.your-company-site.com/fileadmin/company_logo.png" }, "sameAs":[ "http:\\www.facebook.com\yourSite", "http:\\www.youtube.com\user\yourSite", "http:\\www.pinterest.com\yourSite\" ] } </script>
示例2 - 渲染多个实体
TypoScript:
lib.structuredDataMarkupExample < lib.structuredDataMarkup
lib.structuredDataMarkupExample {
settings {
entity {
className = Portrino\PxSemantic\SchemaOrg\Question
id = TEXT
id.data = field:uid
}
processors {
0 {
className = Portrino\PxSemantic\Processor\ExampleProcessor
}
}
}
}
Fluid:
<f:for each="{entities}" as="entity"> {entity -> f:cObject(typoscriptObjectPath: 'lib.structuredDataMarkupExample')} </f:for>
示例3 - 创建LinkedData REST API
从版本2.0.0开始,可以配置基于Hydra Core Vocabulary (http://www.hydra-cg.com/spec/latest/core/)的LinkedData REST API
TypoScript:
- 在您的页面树中创建一个名为“api (123)”的页面,以更好地进行realurl配置
constants.txt
plugin.tx_pxsemantic {
settings {
rest {
pid = 123
}
}
}
}
- 现在为您的rest api的每个端点配置资源到实体的映射,如下面的简单示例所示
- 对于示例1和示例2中的结构化数据标记,您可以使用(重新)使用
processors
将您的资源(即领域模型)转换为http://schema.org/实体
setup.txt
plugin.tx_pxsemantic {
settings {
rest {
endpoints {
pages {
entity = Portrino\PxSemantic\SchemaOrg\CreativeWork
resource = Portrino\PxSemantic\Domain\Model\Page
processors {
0 {
className = Portrino\PxSemantic\Processor\PageProcessor
}
}
}
}
}
}
}
}
- 添加此realurl配置以使您的rest API的URI更简洁
realurl_conf.php
...
'fixedPostVars' => [
'_DEFAULT' => [],
'api' => [
[
'GETvar' => 'type',
'valueMap' => [
'structured-data' => 1475825939,
'structured-data-contexts' => 1476721757,
'vocab' => 1476770090
]
],
[
'GETvar' => 'tx_pxsemantic_hydraapi[endpoint]',
'valueMap' => [
'recipes' => 'recipes',
'pages' => 'pages'
],
'noMatch' => 'bypass'
],
[
'GETvar' => 'tx_pxsemantic_hydracontext[context]',
'valueMap' => [
'Entrypoint' => 'Entrypoint',
],
'noMatch' => 'bypass'
],
[
'cond' => [
'prevValueInList' => 'pages'
],
'GETvar' => 'tx_pxsemantic_hydraapi[uid]',
'lookUpTable' => [
'table' => 'pages',
'id_field' => 'uid',
'alias_field' => 'uid'
],
'optional' => true
]
],
'123' => 'api'
],
...
-
现在您可以通过:http://domain.tld/api/structured-data/pages/调用您的Linked Data REST API,它将呈现为PagedCollection (http://www.hydra-cg.com/spec/latest/core/#collections)
-
将此URL添加到Hydra控制台(http://www.markus-lanthaler.com/hydra/console/)将为您提供一份优秀的Hydra文档
更多示例
作者
还可以查看参与此项目的贡献者列表。