I'm trying to use Dingo API to implement RESTful backend APIs for our webapp. Best practices say that response payload should contain links
to the returned resource objects. It can be in the form of _link
or href
. I don't see a way to include this in the response without handcrafting resource URL. I would like to have the response something like...
[
{
'person': "Joe",
'_link': 'http://api.mydomain.com/v1/persons/2'
},
{
'person': "Pat",
'_link': 'http://api.mydomain.com/v1/persons/3'
}
]
Is there a way I can include a resource link in the response?
Are you using transformations to generate your response data. Dingo API uses Fractal as the default transformation layer. So you will need a PersonTransformer for instance as below:
<?php
namespace App\Transformer;
use App\Model\Person;
use League\Fractal;
class PersonTransformer extends Fractal\TransformerAbstract
{
public function transform(Person $person)
{
return [
'id' => (int) $person->id,
'person' => $person->name,
'links' => [
[
'rel' => 'self',
'uri' => '/persons/' . $person->id,
],
],
];
}
}
You can refer to the following link for more information about transformations and how to use them specific to Fractal. Fractal Transformers