
api-platform是一个强大的框架,用于快速构建restful和graphql api。它默认支持多种数据格式的输入输出,如json-ld、json、xml等,并通过内置的序列化器和内容协商机制高效运作。然而,在某些场景下,我们需要为api资源提供非标准的数据格式,例如,为一张发票资源提供其对应的pdf文档。
传统上,开发者可能会尝试通过Api-Platform的itemOperations或collectionOperations来定义自定义路由,并指定output_formats为application/pdf。例如,以下代码片段展示了这种尝试:
// src/Entity/Invoice.php
#[ApiResource(
itemOperations: [
'get', 'put', 'patch', 'delete',
'get_document' => [
'method' => 'GET',
'path' => '/invoices/{id}/document',
'controller' => DocumentController::class,
'output_formats' => ['application/pdf'],
'read' => false, // 避免Api-Platform尝试序列化整个Invoice对象
],
]
)]
class Invoice
{
// ...
}
// src/Controller/DocumentController.php
#[AsController]
class DocumentController extends AbstractController
{
private InvoiceDocumentService $documentService;
public function __construct(InvoiceDocumentService $invoiceDocumentService)
{
$this->documentService = $invoiceDocumentService;
}
public function __invoke(Invoice $data): string
{
// 返回PDF内容字符串
return $this->documentService->createDocumentForInvoice($data);
}
}这种方法的问题在于,Api-Platform的output_formats机制主要用于指定数据的序列化格式(如JSON、XML),它期望有一个对应的序列化器来处理数据对象并将其转换为指定的MIME类型。对于application/pdf这种二进制文件流,Api-Platform默认并没有内置的序列化器,因此会报错提示不支持该MIME类型。即使尝试注册自定义编码器,也会引入不必要的复杂性,因为它需要处理原始数据流而非结构化数据。
解决上述问题的更简洁、更符合“关注点分离”原则的方法是:将PDF文档的生成和提供视为资源的一个“关联操作”或“属性”,并将其处理逻辑解耦到一个独立的Symfony控制器中,而不是强行集成到Api-Platform的序列化流程。
核心思想是:Api-Platform负责提供结构化的数据API,而文件下载则由一个标准的Symfony控制器来处理。
首先,在您的ApiResource实体(例如Invoice)中添加一个计算属性,该属性返回PDF文档的访问URL。这样,当客户端获取Invoice资源时,可以从其属性中直接获取到PDF文档的下载链接。
// src/Entity/Invoice.php
use ApiPlatform\Metadata\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
#[ORM\Entity]
#[ApiResource(
// ... 其他ApiResource配置
normalizationContext: ['groups' => ['invoice:read']]
)]
class Invoice
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private ?int $id = null;
// ... 其他Invoice属性
public function getId(): ?int
{
return $this->id;
}
/**
* 获取此发票PDF文档的下载URL。
*
* @Groups({"invoice:read"}) // 确保此属性在序列化时被包含
*/
public function getDocumentUrl(): string
{
// 假设您的PDF下载路由是 /invoices/{id}/document
return "/invoices/{$this->id}/document";
}
// ... 其他getter/setter
}通过#[Groups({"invoice:read"})]注解,确保当Invoice对象被序列化为API响应时,documentUrl属性会被包含在内。客户端获取发票详情后,可以直接使用这个URL来下载PDF。
接下来,创建一个标准的Symfony控制器来处理PDF文档的实际生成和文件响应。这个控制器将完全独立于Api-Platform的内部机制,可以灵活地处理文件流。
// src/Controller/InvoiceDocumentController.php
namespace App\Controller;
use App\Entity\Invoice;
use App\Service\InvoiceDocumentService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; // 如果使用旧版Symfony或手动配置
class InvoiceDocumentController extends AbstractController
{
private InvoiceDocumentService $documentService;
public function __construct(InvoiceDocumentService $invoiceDocumentService)
{
$this->documentService = $invoiceDocumentService;
}
/**
* 下载指定发票的PDF文档。
*
* @Route("/invoices/{id}/document", name="app_invoice_document_download", methods={"GET"})
* @ParamConverter("invoice", class="App\Entity\Invoice") // 自动将{id}转换为Invoice对象
*/
public function downloadDocument(Invoice $invoice): Response
{
// 1. 调用服务生成PDF文件路径或内容
// 假设InvoiceDocumentService::createDocumentForInvoice返回一个文件路径
$pdfFilePath = $this->documentService->createDocumentForInvoice($invoice);
// 2. 创建BinaryFileResponse响应
$response = new BinaryFileResponse($pdfFilePath);
// 3. 设置响应头,强制浏览器下载文件
$response->setContentDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
'invoice_' . $invoice->getId() . '.pdf'
);
// 4. 设置内容类型
$response->headers->set('Content-Type', 'application/pdf');
// 5. 可选:删除临时文件(如果服务生成的是临时文件)
// $response->deleteFileAfterSend(true);
return $response;
}
}在这个控制器中:
如果您的InvoiceDocumentService直接返回PDF内容的二进制字符串,您可以改用Response对象:
// ...
use Symfony\Component\HttpFoundation\Response;
// ...
public function downloadDocument(Invoice $invoice): Response
{
$pdfContent = $this->documentService->createDocumentContentForInvoice($invoice); // 假设返回二进制字符串
$response = new Response($pdfContent);
$response->headers->set('Content-Type', 'application/pdf');
$response->headers->set('Content-Disposition', 'attachment; filename="invoice_' . $invoice->getId() . '.pdf"');
return $response;
}通过#[Route]注解,我们直接在控制器方法上定义了路由。#[ParamConverter]则负责将路由参数自动转换为实体对象,省去了手动从仓库中查找的步骤。这种方式是Symfony中处理实体参数的推荐方法。
// src/Controller/InvoiceDocumentController.php
// ...
use OpenApi\Attributes as OA; // 假设您使用nelmio/api-doc-bundle 和 openapi-php
class InvoiceDocumentController extends AbstractController
{
// ...
/**
* 下载指定发票的PDF文档。
*/
#[Route("/invoices/{id}/document", name="app_invoice_document_download", methods={"GET"})]
#[ParamConverter("invoice", class="App\Entity\Invoice")]
#[OA\Response(
response: 200,
description: 'Returns the PDF document for the invoice',
content: new OA\MediaType(mediaType: 'application/pdf')
)]
#[OA\Parameter(
name: 'id',
in: 'path',
required: true,
description: 'ID of the invoice',
schema: new OA\Schema(type: 'integer')
)]
#[OA\Tag(name: 'Invoices')] // 将此端点归类到Invoices标签下
public function downloadDocument(Invoice $invoice): Response
{
// ...
}
}无论采用哪种方法,对文件下载路由进行严格的安全性检查都是至关重要的。您不希望任何用户都能下载任何发票的PDF。Symfony提供了强大的安全组件来处理权限控制:
// src/Controller/InvoiceDocumentController.php
// ...
use Symfony\Component\Security\Http\Attribute\IsGranted; // Symfony 6.2+
class InvoiceDocumentController extends AbstractController
{
// ...
#[Route("/invoices/{id}/document", name="app_invoice_document_download", methods={"GET"})]
#[ParamConverter("invoice", class="App\Entity\Invoice")]
#[IsGranted('VIEW', subject: 'invoice', message: 'You are not authorized to view this invoice document.')]
public function downloadDocument(Invoice $invoice): Response
{
// ...
}
}在这个例子中,#[IsGranted('VIEW', subject: 'invoice')]会触发您的安全系统(例如一个InvoiceVoter),检查当前用户是否具有“VIEW”权限来访问这个特定的$invoice对象。
通过将自定义的文件输出逻辑从Api-Platform的核心资源定义中解耦出来,并将其迁移到独立的Symfony控制器中,我们可以:
这种方法提供了一个健壮且易于扩展的解决方案,用于在Api-Platform项目中集成各种自定义文件输出功能。
以上就是Api-Platform:为资源集成自定义PDF文档下载功能的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号