Api Platform conference
Register now

Both HydraAdmin and OpenApiAdmin leverage introspection of the API schema to discover its capabilities, like filtering and sorting.

They also detect wether the API has real-time capabilities using Mercure, and automatically enable it if it does.

Lastly, API Platform Admin has native support for the popular Schema.org vocabulary, which enables it to automatically use the field type matching your data, or display a related resource’s name instead of its IRI.

# Adding Filtering Capabilities

You can add the ApiFilter attribute to an API Platform resource to configure a filter on a property.

For instance, here is how configure filtering on the id, title and author properties of a Book resource:

<?php
// api/src/Entity/Book.php
namespace App\Entity;

use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use Doctrine\ORM\Mapping as ORM;

#[ApiResource]
#[ORM\Entity]
#[ApiFilter(SearchFilter::class, properties: [
    'id' => 'exact', 
    'title' => 'ipartial', 
    'author' => 'ipartial'
])]
class Book
{
    // ...
}

If you are using the guessers, the Admin will automatically update the Book list view to include a filter on the selected properties.

Filtering on the title property

Tip: Learn more about the ApiFilter attribute in the core documentation.

# Adding Sorting Capabilities

You can also use the ApiFilter attribute on an API Plaform resource to configure sorting.

For instance, here is how to configure sorting on the id, isbn, title, author and publicationDate properties of a Book resource:

<?php
// api/src/Entity/Book.php
namespace App\Entity;

use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use Doctrine\ORM\Mapping as ORM;

#[ApiResource]
#[ORM\Entity]
#[ApiFilter(OrderFilter::class, properties: [
    'id' => 'ASC', 
    'isbn' => 'ASC', 
    'title' => 'ASC', 
    'author' => 'ASC', 
    'publicationDate' => 'DESC'
])]
class Book
{
    // ...
}

If you are using the guessers, the Admin will automatically update the Book list view to make the selected columns sortable.

Sorting by the title property

Tip: Learn more about the ApiFilter attribute in the core documentation.

# Enabling Real-Time Updates

You can use the mercure attribute to hint API Platform that it must dispatch the updates regarding the given resources to the Mercure hub:

<?php
// api/src/ApiResource/Book.php with Symfony or app/ApiResource/Book.php with Laravel
namespace App\ApiResource;

use ApiPlatform\Metadata\ApiResource;

#[ApiResource(mercure: true)]
class Book
{
    // ...
}

Make sure you have configured your dataProvider to support Mercure.

Tip: Learn more about how to use the Mercure Protocol in the API Platform Core documentation.

# About Schema.org

API Platform Admin has native support for the popular Schema.org vocabulary.

Schema.org is a collaborative, community activity with a mission to create, maintain, and promote schemas for structured data on the Internet, on web pages, in email messages, and beyond.

To leverage this capability, your API must use the JSON-LD format and the appropriate Schema.org types. The following examples will use API Platform Core to create such API, but keep in mind that this feature will work with any JSON-LD API using the Schema.org vocabulary, regardless of the used web framework or programming language.

By default, IRIs of related objects are displayed in lists and forms. However, it is often more user-friendly to display a string representation of the resource (such as its name) instead of its ID.

To configure which property should be shown to represent your entity, map the property containing the name of the object with the https://schema.org/name type:

// api/src/Entity/Person.php
...

#[ApiProperty(iris: ["https://schema.org/name"])]
private $name;

...
With IRIWith Resource Name
Related Record With IRIRelated Record  With Resource Name

# Emails, URLs and Identifiers

Besides, it is also possible to use the documentation to customize some fields automatically while configuring the semantics of your data.

The following Schema.org types are currently supported by API Platform Admin:

  • https://schema.org/email: the field will be rendered using the <EmailField> React Admin component
  • https://schema.org/url: the field will be rendered using the <UrlField> React Admin component
  • https://schema.org/identifier: the field will be formatted properly in inputs

Note: if you already use validation on your properties, the semantics are already configured correctly (see the correspondence table)!

# Next Step

Learn how to tweak the generated Admin by Customizing the Guessers.

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