【问题标题】:How to set variable in path - API Platform如何在路径中设置变量 - API 平台
【发布时间】:2018-04-26 13:33:15
【问题描述】:

我正在 Symfony 中创建小型 API,并且正在使用 Api Platform。

/src/BooksBundle/Resources/config/api_resources/resources.xml 我将路径设置为:

<collectionOperations>
    <collectionOperation name="get">
        <attribute name="method">GET</attribute>
        <attribute name="path">/{shop}/books</attribute>
        <attribute name="normalization_context">
            <attribute name="groups">
                <attribute>books_list</attribute>
                <attribute>list</attribute>
            </attribute>
        </attribute>
    </collectionOperation>
</collectionOperations>

swagger.yml,我已经设置了路径和参数

paths:
/api/{shop}/books:
  get:
    tags:
      - Book
    operationId: getBookCollection
    produces:
      - application/json
      - application/ld+json
      - text/html
    summary: Retrieves the collection of Book resources.
    responses:
      '200':
        description: Book collection response
        schema:
          type: array
          items:
            $ref: '#/definitions/Book-book_list'
      '401':
        description: Unauthorized
    parameters:
      - name: title
        in: query
        required: false
        type: string
      - name: shop
        in: path
        required: true
        type: string

我的路线例如:127.0.0.1/api/shopName/books?title=sometitle 作为回应,我收到此错误:Unable to generate an IRI for "BookBundle\Entity\Book".

我调查了异常,消息是: Some mandatory parameters are missing ("shop") to generate a URL for route "api_books_get_collection".

所以我需要一种通过 API 平台中的路径发送参数的方法,由于某种原因,这不起作用。


仅供参考,为什么我认为这是配置问题。我的参数 Shop in entity Book,有 getter 和 setter,如果我在实体中输入固定值,从配置和路由中删除参数,url 127.0.0.1/api/books?title=sometitle 完美运行,我得到完整的 Hydra Json 响应。

【问题讨论】:

    标签: symfony swagger api-platform.com


    【解决方案1】:

    您必须在 API 平台中以一种宁静的方式调用您的路由,如下所示:

    GET /resource-name/id/resource-name/id
    

    所以你应该这样做:

    GET /shops/{shop-identifier}/books?title=sometitle
    

    让我知道这是否可行,并始终记住遵循 REST 标准。

    【讨论】:

      猜你喜欢
      • 2012-09-20
      • 1970-01-01
      • 1970-01-01
      • 2015-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-21
      • 1970-01-01
      相关资源
      最近更新 更多