thephpleague / fractal

Output complex, flexible, AJAX/RESTful data structures.
fractal.thephpleague.com
MIT License
3.52k stars 352 forks source link

Subresource endpoint #301

Open jpkleemans opened 8 years ago

jpkleemans commented 8 years ago

Lets say I have the following endpoint: /authors/{author_id}/books/{book_id}. How can I create the output for this endpoint with Fractal, and keeping the correct links?

I was thinking about something like:

$item = new Item($book, $bookTransformer, 'authors.books');

But this outputs /authors.books/{book_id} as self-link. Which makes sense when you see https://github.com/thephpleague/fractal/blob/master/src/Serializer/JsonApiSerializer.php#L77

lukasoppermann commented 8 years ago

I don't think this is the idea. You are returning a link to the actual resource so just books.

This way you get the correct link of /books/{book_ID}

seant23 commented 7 years ago

I think this fine to do. I've seen many APIs that do this. It's situational of course but you should be able to do this.

I ended up created a propietaryJsonApiSerializer, that does two things:

  1. It allows me to set the resource key in the transformer, simply because having to add in the resource key for every single item/collection call seems really shitty.

  2. I added a check for the data array. If it includes _resourceAddress, then I use that instead of the auto generated $resourceKey/$id links.

https://gist.github.com/seant23/2e049f73b3cf9b52635a3cd0b9fc9c50

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 4 weeks if no further activity occurs. Thank you for your contributions.