becklyn / schema-org
1.7.0
2022-02-17 15:54 UTC
Requires
- php: >=7.4
- ext-json: *
- becklyn/rad-bundles: ^1.1.0
- symfony/config: ^5.4.3 || ^6.0.3
- symfony/dependency-injection: ^5.4.3 || ^6.0.3
- symfony/http-kernel: ^5.4.3 || ^6.0.3
- twig/twig: ^3.3.8
Requires (Dev)
- roave/security-advisories: dev-master
- symfony/phpunit-bridge: ^5.1.5
README
此包提供了一种薄集成,用于渲染与Schema.org兼容的JSON-LD元数据标签。
创建schema
在您可以在模板中渲染元数据之前,您需要为每个您想生成JSON-LD schema的实体类型实现一个SchemaBuilderInterface
。
一个SchemaBuilderInterface
实现的示例将如下所示,它将MyNewsArticle
转换为Schema.org的Article
。
use App\Entity\MyNewsArticle; use Becklyn\SchemaOrg\Data\Article; use Becklyn\SchemaOrg\Data\Organization; use Becklyn\SchemaOrg\Data\SchemaOrgDataInterface; use Becklyn\SchemaOrg\SchemaBuilder\SchemaBuilderInterface; class MyNewsSchemaBuilder implements SchemaBuilderInterface { /** * @inheritDoc */ public function supports ($entity, ?string $usage = null, array $context = []) : bool { return $entity instanceof MyNewsArticle; } /** * @inheritDoc */ public function buildSchema ($entity, ?string $usage = null, array $context = []) : ?SchemaOrgDataInterface { \assert($entity instanceof MyNewsArticle); // Don't generate metadata for unpublished news if (!$entity->isPublished()) { return null; } return (new Article()) ->withEditor($entity->getAuthor()) ->withAbout($entity->getTeaserText()) ->withArticleBody($entity->getTextContent()) ->withPublisher( (new Organization()) ->withName("Awesome Publisher") ->withEmail("news@example.com") ) ->withLicense("Creative Commons Attribution-ShareAlike 3.0") ; } }
渲染元数据标签
要渲染JSON-LD元数据标签,调用Twig函数{{ schema_org_meta_data(myAppEntity) }}
,并传递可选的usage
和context
参数,如果您需要根据当前的渲染上下文调整输出。