【问题标题】:Symfony: Output example image in nelmio api docs?Symfony:在 nelmio api 文档中输出示例图像?
【发布时间】:2017-12-06 00:55:11
【问题描述】:

我正在使用Nelmio Api Doc 为我的 API 生成文档。我刚刚添加了一个新端点,它根据 id 参数返回图像/png 文件。我如何在我的 api 文档中最好地表示这个响应?理想情况下,我希望在此端点的文档的示例响应部分中显示示例图像。但是我可以用 nelmio 做到这一点吗?请看下面:

/**
 *
 * ### Example Response ###
 *     [
 *         {TELL NELIMO TO OUTPUT EXAMPLE IMAGE?}
 *     ]
 *
 * @Route("/image/{id}", name="image_get", requirements={"id": "\d+"})
 * @Method("GET")
 *
 * @ApiDoc(
 *  section="image",
 *  description="Fetch image.",
 *  headers={
 *     {
 *          "name" : "api-key",
 *          "description"="Token the client needs to provide when making API calls.",
 *          "required"="true"
 *     }
 *  },
 *  requirements={
 *      {
 *          "name"="id",
 *          "dataType"="integer",
 *          "requirement"="\d+",
 *          "description"="ID of the image you wish to retrieve."
 *      }
 *  },
 *  parameters={},
 *  filters={},
 *  statusCodes={
 *      200="Returned when successful",
 *      400={
 *        "Returned when bad request",
 *      },
 *      401={
 *        "Returned when unauthorized",
 *      },
 *      404={
 *        "Returned when not found",
 *      }
 *   }
 * )
 */
public function getAction($id, Request $request)
{
    /** @var ImageRepository $imageRepository */
    $imageRepository = $this->get('api.repository.image');

    /** @var Image $image */
    $image = $imageRepository->fetchById($id);

    if(empty($image->getId())){
        $problem = new ApiProblem("Image not found", "E_NOT_FOUND");
        $problem->setDetail("A image could not be found.");
        $problem->setInstance($request->getUri());
        return new Response($problem->asJson(), Response::HTTP_NOT_FOUND);
    }

    /** @var string $file */
    $file = file_get_contents(__DIR__ . '/../../../../app/Resources/img/' . $flag->getImg());

    return new Response($file, 200, [
        'Content-Type' => 'image/png',
        'Content-Disposition' => 'inline; filename="'.$image->getImg().'"'
    ]);
}

【问题讨论】:

  • Markdown 老兄...
  • 感谢@mike 完成了这项工作!
  • 纯 HTML 也可以解决问题

标签: php symfony swagger nelmioapidocbundle


【解决方案1】:

![alt text](https://some_image.png)这样在评论中使用markdown,它将在nelmio api doc中输出。

【讨论】:

    猜你喜欢
    • 2016-03-27
    • 2020-05-05
    • 2019-08-28
    • 2018-06-17
    • 2023-02-05
    • 1970-01-01
    • 2013-03-04
    • 2013-04-17
    • 1970-01-01
    相关资源
    最近更新 更多