You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<?php
namespace App\Entity;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use Doctrine\ORM\Mapping as ORM;
use App\Repository\KeyRepository;
use App\Dto\KeyOutput;
use App\State\KeyOutputProvider;
use ApiPlatform\Metadata\Post;
#[ApiResource(
shortName: 'Key_new',
operations: [
new Post(),
],
)]
#[Get(
uriTemplate: '/keys_new/{id}',
shortName: 'Key_new',
output: KeyOutput::class,
provider: KeyOutputProvider::class,
openapi: true,
)]
#[ORM\Entity(repositoryClass: KeyRepository::class)]
class KeyNew {
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
public ?int $id = null;
#[ORM\Column(length: 255)]
private ?string $title = null;
public function getId(): int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
}
DTO:
<?php
namespace App\Dto;
use App\State\KeyOutputProvider;
use ApiPlatform\Metadata\Get;
class KeyOutput
{
public int $id;
public string $title;
}
State provider:
<?php
namespace App\State;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\Dto\KeyOutput;
use App\Repository\KeyRepository;
final class KeyOutputProvider implements ProviderInterface
{
public function __construct(private ProviderInterface $itemProvider, private KeyRepository $keyRepository)
{
}
/**
* {@inheritDoc}
*/
public function provide(Operation $operation, array $uriVariables = [], array $context = []) : object | array
{
$key = $this->keyRepository->find($uriVariables['id']);
$output = new KeyOutput();
$output->id = $key->getId();
$output->title = $key->getTitle();
return $output;
}
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I'm using the API platform v3.1.14
I'm using a DTO to return a custom output for an entity. However it is generating a skolem iri instead of the Entity of the id for the @id attribute
I've tried the suggestions in api-platform/core#5451 but it is not working.
DTO:
State provider:
output:
{ "@context": { "@vocab": "https://sf.local.com/tts-api/docs.jsonld#", "hydra": "http://www.w3.org/ns/hydra/core#", "id": "KeyOutput/id", "title": "KeyOutput/title" }, "@type": "KeyOutput", **"@id": "/tts-api/.well-known/genid/8b4f34f659946eb5666c",** "id": 1, "title": "testa" }
I want in the @id to get "/tts-api/key_new/1"
Any idea ?
Beta Was this translation helpful? Give feedback.
All reactions