【发布时间】:2019-08-07 12:16:53
【问题描述】:
我正在使用 API 平台,我正在寻找一些东西。 我做了一个这样的人实体:
class Person
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\Column(type="string", length=255)
*/
private $firstname;
/**
* @ORM\Column(type="datetime")
*/
private $birthdate;
}
为字段定义组:
App\Entity\Person:
attributes:
id:
groups: ['private']
name:
groups: ['public']
firstname:
groups: ['public']
birthdate:
groups: ['public']
我还明确指出,如果我想要该资源的所有集合,则只应序列化公共字段:
App\Entity\Person:
collectionOperations:
get:
filters: ['search_filter']
normalization_context:
groups: ['public']
formats: ['json']
如您所见,我应用了搜索过滤器。在这种情况下,我可以从精确为查询参数的字段中检索资源。
但是,我想仅对公共字段应用此过滤器。 所以我不希望http://localhost/api/people?id=1 请求有效,因为 id 字段是私有。
我发现可以将想要作为 SearchFilter 参数的字段精确化,但改为精确化组名会更有用,因为我打算与更多组合作。
我试图查看 GroupFilters,但它对我没有帮助,因为它是一个序列化器过滤器...
你有什么推荐给我的?
【问题讨论】:
标签: symfony serialization api-platform.com