Getting the IRI of an entity with API Platform

Published on 2022-03-14 • Modified on 2022-03-14

This snippet shows how to get the IRI of an entity with API Platform (and the opposite!). I have added this snippet because I noticed nothing about this in the API Platform documentation (and someone asked on Slack too). This IriConverter service can be helpful, for example, if you want to send messages because the IRI contains both the ID and the type of resource to process. The reverse function getItemFromIri() can be useful; for example, in a message handler, you directly get the Doctrine record without bothering with the suitable repository to use.


<?php

declare(strict_types=1);

namespace App\Controller\Snippet;

use ApiPlatform\Api\IriConverterInterface;
use App\Entity\Article;

/**
 * I am using a PHP trait to isolate each snippet in a file.
 * This code should be called from a Symfony controller extending AbstractController (as of Symfony 4.2)
 * or Symfony\Bundle\FrameworkBundle\Controller\Controller (Symfony <= 4.1).
 * Services are injected in the main controller constructor.
 *
 * @property IriConverterInterface $iriConverter
 */
trait Snippet193Trait
{
    public function snippet193(): void
    {
        $snippet = $this->articleData->getSnippetById(193);
        $snippetIri = $this->iriConverter->getIriFromResource($snippet);
        echo 'IRI: '.$snippetIri;

        unset($snippet);
        echo PHP_EOL;

        /** @var Article $itemFromIri */
        $itemFromIri = $this->iriConverter->getResourceFromIri((string) $snippetIri);
        echo 'Snippet: '.$itemFromIri->getSlug().' ('.$itemFromIri->getId().')';

        // That's it! 😁
    }
}

 Run this snippet  ≪ this.showUnitTest ? this.trans.hide_unit_test : this.trans.show_unit_test ≫  More on Stackoverflow   Read the doc  More on the web

  Work with me!

<?php

declare(strict_types=1);

namespace App\Tests\Integration\Controller\Snippets;

use ApiPlatform\Api\IriConverterInterface;
use ApiPlatform\Symfony\Routing\IriConverter;
use App\Data\ArticleData;
use App\Entity\Article;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

/**
 * @see Snippet193Trait
 */
final class Snippet193Test extends KernelTestCase
{
    private ArticleData $articleData;
    private IriConverterInterface $iriConverter;

    protected function setUp(): void
    {
        /** @var ArticleData $articleData */
        $articleData = self::getContainer()->get(ArticleData::class);
        $this->articleData = $articleData;

        /** @var IriConverter $iriConverter */
        $iriConverter = self::getContainer()->get(IriConverterInterface::class);

        $this->iriConverter = $iriConverter;
    }

    /**
     * @see Snippet193Trait::snippet193
     */
    public function testSnippet193(): void
    {
        $snippet = $this->articleData->getSnippetById(193);
        $snippetIri = $this->iriConverter->getIriFromResource($snippet);
        self::assertSame('/api/articles/193', $snippetIri);

        /** @var Article $itemFromIri */
        $itemFromIri = $this->iriConverter->getResourceFromIri($snippetIri);
        self::assertSame($snippetIri, $this->iriConverter->getIriFromResource($itemFromIri));
    }
}