Api-platform 2.7+如何通过YAML定义子资源路由配置?
Api-platform 2.7+ 类子资源的YAML配置实现
以下是对应你提供的PHP Attribute代码的YAML配置方案,两种写法可选:
分文件配置(推荐,资源拆分更清晰)
Document 实体配置(config/api_platform/resources/Document.yaml)
App\Entity\Document: attributes: ~
Preview 实体配置(config/api_platform/resources/Preview.yaml)
App\Entity\Preview: uriTemplate: '/documents/{id}/previews' operations: ApiPlatform\Core\Action\GetCollectionAction: ~ uriVariables: id: toProperty: document fromClass: App\Entity\Document
单文件集中配置(config/packages/api_platform.yaml)
api_platform: resources: App\Entity\Document: ~ App\Entity\Preview: uriTemplate: '/documents/{id}/previews' operations: ApiPlatform\Core\Action\GetCollectionAction: ~ uriVariables: id: toProperty: document fromClass: App\Entity\Document
配置说明
uriTemplate:定义子资源的访问路径,对应PHP Attribute里的同名参数operations:指定该资源路径对应的操作,GetCollectionAction等价于PHP里的new GetCollection()uriVariables:配置路径变量的关联规则,toProperty对应Preview实体中关联Document的属性名,fromClass指定关联的实体类
内容的提问来源于stack exchange,提问作者lifo
相关产品推荐
相关产品推荐

