API presenters
This will determine what attributes of the resource are returned to the client in the response. You can have different sets of attributes be included in the response based on the action (e.g. create, detail, delete, list, update) or a custom format.
Define your presenter by creating a file api/presenters/SwordPresenter
:
By default, whenever a sword is being returned in an API response, it will return a sword object in
JSON format with the id
, name
, and strenght
keys.
Register different formats for different actions
If you want the resource to return different data, you can register unique formats and the attributes that format should return:
This will only return the id
attribute when making an API call to collections endpoint
/swords
to retrieve a list of sword resources.
Register custom formats
You may want to define a custom format that can return specific attributes when making an API
call by including the _format
URL parameter. For example, you can present specific fields by
calling the GET /swords?_format=all_attributes
endpoint.
Note: be sure to permit reading these attributes in the SwordPolicy
.
Was this page helpful?