I have been API platform with Symfony since the first beta version.
I am now using it with Laravel and I am trying to differentiate the properties (aka Eloquent attributes) that are exposed on the item and collection routes. Using the $hidden
property seems to be the only way, and it feels a bit limited. I even tried to decorate the CollectionProvider to dynamically call setHidden
on all the models of the collection, but it is too late in the process since it has already been extracted ahead of the provider. Also, decorating the provider is not technically possible unless the new provider is also manually declared on each operation of all resources because the default value for the provider in the Operation attribute is hardcoded and does not seem to come from the container.
Using an output DTO would be one way to achieve that, but I'm hoping I could go without the extra work of defining custom DTOs and providers and manually creating a new Paginator for collection routes just for this issue (also, the hydra attributes are messed up when using DTOs).
What thing similar to the normalizationContext
with the Laravel version of API platform can I use to differentiate the Eloquent attributes that are exposed?
I figured out how to define the properties that I want to expose on each route.
new GetCollection(
normalizationContext: [AbstractNormalizer::IGNORED_ATTRIBUTES => [
'hidden_property_1',
'hidden_property_2',
]],
),
// or
new GetCollection(
normalizationContext: [AbstractNormalizer::ATTRIBUTES => [
'property_1',
'property_2',
]],
),
It works, but it is not reflected in the Open API documentation. I feel like it's more of a workaround than an actual solution.