繁体   English   中英

Symfony:在nelmio api文档中输出示例图像?

[英]Symfony: Output example image in nelmio api docs?

我正在使用Nelmio Api Doc为我的API生成文档。 我刚刚添加了一个新的终结点,该终结点根据id参数返回image / 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就像![alt text](https://some_image.png) ,它将在nelmio api文档中输出。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM