Why do I get "No item route associated with the type" when using Api-Platform? Why do I get "No item route associated with the type" when using Api-Platform? symfony symfony

Why do I get "No item route associated with the type" when using Api-Platform?


TLDR:

For all resources, you need to have the basic GET /resource/{id} route.



The error message is very explicit:

No item route associated with the type "App\Entity\Answer"

If you check your resource definition for Answer you will see:

itemOperations:      put:        security: 'is_granted("ROLE_ADMIN") or object.owner == user'        security_message: 'Only the authenticated user or administrators can update answers'      delete:        security: 'is_granted("ROLE_ADMIN")'        security_message: 'Only administrators can delete answers'

So you basically only have delete and update routes, but do not have a basic item retrieve route (GET /api/answers/123). By defining the routes explicitly, whatever route you do not define you disable.

That is confirmed with the output from the API docs:enter image description here

Without that, it is impossible to get a valid IRI for the resource. Without a valid GET route for a resource the system cannot generate a URL for each resource item, and since those URLs/IRIs are used as identifiers by the system, these should always be defined.

You just need to add the configuration for the item route back:

itemOperations:      put:        security: 'is_granted("ROLE_ADMIN") or object.owner == user'        security_message: 'Only the authenticated user or administrators can update answers'      delete:        security: 'is_granted("ROLE_ADMIN")'        security_message: 'Only administrators can delete answers'      get:        security: 'is_granted("ROLE_USER")'        security_message: 'Only the authenticated user can get answers'