はじめに
api-platformを使っていて、躓いた部分のメモです。
問題
Symfonyのapi-platformで開発をしていて、No item route associated with the type "App\Entity\クラス名"
というエラーが表示された。
状況
MySQLのViewで作成したテーブルなので、ReadOnlyのつもりで、collectionOperationsのGETのみとして以下の設定を行った。
/**
* @ApiResource(
* collectionOperations={
* "get"
* },
* itemOperations={
* },
* )
* @ORM\Entity(repositoryClass=hogehoge::class, readOnly=true)
* @ORM\Table(name="view_hogehoge")
*/
以下の画面でも正常に表示されているので良さそうな感じ。
しかし、実際にAPIを呼び出すと、エラーを吐く
回答
アノテーションの以下の書き方がダメ。
Issuに上がっていた。
https://github.com/api-platform/core/issues/3501
* itemOperations={
* },
こちらのように書くと動作する
/**
* @ApiResource(
* collectionOperations={
* "get"
* },
* itemOperations={
* "get"={
* "method"="GET",
* "controller"=NotFoundAction::class,
* "read"=false,
* "output"=false,
* },
* },
* )
* @ORM\Entity(repositoryClass=hogehoge::class, readOnly=true)
* @ORM\Table(name="view_hogehoge")
*/
言い訳
R/Wできるテーブルで使っているので、R/Oの場合にちょこっと書き方を変えたら躓いてしまった。
しかも、利用者が少ないのか、日本語での回答がなかったので書いてみました。