admin管理员组文章数量:1336304
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 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?
1 Answer
Reset to default 0I 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.
版权声明:本文标题:laravel - How can I differentiate the exposed properties on the item and the collection routes? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742390853a2465959.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论