Api Platform conference
Register now
v2.4 Using External Vocabularies
API Platform Conference
September 19-20, 2024 | Lille & online

The international conference on the API Platform Framework

API Platform Conference 2024: meet the best PHP, JavaScript and API experts

Learn more about the event, register for the conference, and get ready for two days of inspiration, ideas, and knowledge-sharing with our incredible lineup of renowned specialists and advocates.

Register now

Using External Vocabularies

JSON-LD allows to define classes and properties of your API with open vocabularies such as Schema.org and Good Relations.

API Platform Core provides annotations usable on PHP classes and properties for specifying a related external IRI.

<?php
// api/src/Entity/Book.php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;

/**
 * @ApiResource(iri="http://schema.org/Book")
 */
class Book
{
    // ...

    /**
     * ...
     * @ApiProperty(iri="http://schema.org/name")
     */
    public $name;
    
    // ...
}

The generated JSON for products and the related context document will now use external IRIs according to the specified annotations:

GET /books/22

{
  "@context": "/contexts/Book",
  "@id": "/books/22",
  "@type": "https://schema.org/Book",
  "name": "My awesome book"
}

GET /contexts/Book

{
  "@context": {
    "@vocab": "http://example.com/apidoc#",
    "hydra": "http://www.w3.org/ns/hydra/core#",
    "name": "https://schema.org/name"
  }
}

An extended list of existing open vocabularies is available on the Linked Open Vocabularies (LOV) database.

By default, when using validations API Platform Core will try to define known Schema.org types as IRIs for your properties if you did not provide any in your @ApiProperty annotations. Built-in mapping is:

ConstraintsSchema.org type
Symfony\Component\Validator\Constraints\Urlhttp://schema.org/url
Symfony\Component\Validator\Constraints\Emailhttp://schema.org/email
Symfony\Component\Validator\Constraints\Uuidhttp://schema.org/identifier
Symfony\Component\Validator\Constraints\CardSchemehttp://schema.org/identifier
Symfony\Component\Validator\Constraints\Bichttp://schema.org/identifier
Symfony\Component\Validator\Constraints\Ibanhttp://schema.org/identifier
Symfony\Component\Validator\Constraints\Datehttp://schema.org/Date
Symfony\Component\Validator\Constraints\DateTimehttp://schema.org/DateTime
Symfony\Component\Validator\Constraints\Timehttp://schema.org/Time
Symfony\Component\Validator\Constraints\Imagehttp://schema.org/image
Symfony\Component\Validator\Constraints\Filehttp://schema.org/MediaObject
Symfony\Component\Validator\Constraints\Currencyhttp://schema.org/priceCurrency
Symfony\Component\Validator\Constraints\Isbnhttp://schema.org/isbn
Symfony\Component\Validator\Constraints\Issnhttp://schema.org/issn

You can also help us improve the documentation of this page.

Made with love by

Les-Tilleuls.coop can help you design and develop your APIs and web projects, and train your teams in API Platform, Symfony, Next.js, Kubernetes and a wide range of other technologies.

Learn more

Copyright © 2023 Kévin Dunglas

Sponsored by Les-Tilleuls.coop