the-cocktail / sulu-schema-org-bundle
v1.0.9
2022-07-07 08:22 UTC
Requires
- php: >=7.4
- doctrine/doctrine-bundle: ^1.0 || ^2.0 || ^3.0
- spatie/schema-org: ^2.13 || ^3.0
- sulu/sulu: ^2.1
- symfony/config: ^4.3 || ^5.1
- symfony/dependency-injection: ^4.3 || ^5.1
- symfony/http-foundation: ^4.3 || ^5.1
- symfony/http-kernel: ^4.3 || ^5.1
- twig/twig: ^2.0 || ^3.0
Requires (Dev)
- handcraftedinthealps/zendsearch: ^2.0
- jackalope/jackalope-doctrine-dbal: ^1.4.1
- jackalope/jackalope-jackrabbit: ^1.3
- phpstan/phpstan: ^0.12.18
- phpstan/phpstan-doctrine: ^0.12.10
- phpstan/phpstan-phpunit: ^0.12.6
- phpstan/phpstan-symfony: ^0.12.4
- phpunit/phpunit: ^8.5 || ^9.0
- squizlabs/php_codesniffer: ^3.5
- symfony/browser-kit: ^5.2
- symfony/dotenv: ^4.3 || ^5.0
README
此软件包将 Schema.org 的结构化数据集成到您的项目中。
安装
composer requiere plozmun/sulu-schema-org-bundle
将以下代码插入到您的 "base.html.twig" 中,以便将整个生成的结构化数据自动插入到响应中。
{{ constant('Plozmun\\Bundle\\SuluSchemaOrgBundle\\Factory\\SchemaOrgFactory::TWIG_KEY')|raw }}
现在,您可以为每个模板定义 Schema 类型,为每个模板和元素添加 'sulu.schema_org' 标签。
在 homepage.xml 中的基本示例
<key>homepage</key> <tag name="sulu.schema_org" itemtype="WebSite"/> <view>pages/homepage</view> <controller>Sulu\Bundle\WebsiteBundle\Controller\DefaultController::indexAction</controller> <cacheLifetime>86400</cacheLifetime> <meta> <title lang="en">Homepage</title> <title lang="es">Homepage</title> </meta> <properties> <property name="title" type="text_line" mandatory="true"> <meta> <title lang="en">Title</title> <title lang="es">Título</title> </meta> <params> <param name="headline" value="true"/> </params> <tag name="sulu.schema_org" itemtype="WebSite" itemprop="name"/> <tag name="sulu.rlp.part"/> </property> <property name="url" type="resource_locator" mandatory="true"> <meta> <title lang="es">Slug</title> <title lang="en">Slug</title> </meta> <tag name="sulu.schema_org" itemtype="WebSite" itemprop="url"/> <tag name="sulu.rlp"/> </property> </properties> </template>
生成代码
<script type="application/ld+json"> { "@context":"https:\/\/schema.org", "@type":"WebSite", "name":"Homepage", "url":"\/", "description":"Metadescription", "datePublished":"2020-05-20T12:33:59+02:00" } </script>
有一些有用的选项可以自动分配 SEO 和 Extracts 属性
sulu_schema_org: # image format generated for all image types image_format: "sulu-240x" organization: enabled: true schema: EducationalOrganization uid: main extensions: seo: default: title: property: name type: text_line description: property: description type: text_line Article: description: property: headline type: text_line excerpt: default: icon: property: image type: media_selection
块中的标签
您可以通过根据范围分配字段来在项目的所有块中定义属性。
<type name="hero"> <meta> <title lang="es">01M - Hero</title> <title lang="en">01M - Hero</title> </meta> <properties> <property name="title" type="text_line" mandatory="true"> <meta> <title lang="es">Título</title> <title lang="en">Title</title> </meta> </property> <property name="images" type="media_selection" mandatory="true" maxOccurs="1"> <meta> <title lang="es">Imágenes</title> <title lang="en">Images</title> </meta> <tag name="sulu.schema_org" itemtype="WebSite" itemprop="image"/> </property> </properties> </type>
有时字段在多个范围内使用,那么您可以通过将 'itemtype' 属性改为 "*" 来更改它
<tag name="sulu.schema_org" itemtype="*" itemprop="image"/>
复杂结构
许多模式定义了子对象,例如事件中的地址,因此您必须在模板中注册所有内容以及其范围。属性 itemscope 定义了父对象,因此您必须指定
<tag name="sulu.schema_org" itemtype="Event"/>
<tag name="sulu.schema_org" itemtype="Place" itemscope="Event" itemprop="location" />
<tag name="sulu.schema_org" itemtype="PostalAddress" itemscope="Place" itemprop="address" />
现在,如果您的代码中存在具有 itemtype="PostalAddress 的属性,它将被分配到其模式
<property name="city" type="text_line" mandatory="true"> <meta> <title lang="en">City</title> </meta> <tag name="sulu.schema_org" itemtype="PostalAddress" itemscope="Place" itemprop="addressLocality" /> </property> <property name="zip" type="text_line" mandatory="true"> <meta> <title lang="en">Zip</title> </meta> <tag name="sulu.schema_org" itemtype="PostalAddress" itemscope="Place" itemprop="postalCode" /> </property> ...
面包屑
有一个 Twig 函数可以生成面包屑结构化数据。将其放在 twig 模板中的任何位置
{{ sulu_schema_org('breadcrumb', breadcrumb) }}
组织
如果配置中启用了组织选项,则将定义有 uid 的组织结构化数据注入到结构化数据列表中。您必须指定您组织的类型。 在此处查看一些类型