You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 22:55:17