【发布时间】:2023-03-19 13:35:02
【问题描述】:
我在里面做了一个 Bundle 和一个 REST 控制器。 “index”方法返回JSON格式的数组,没关系:
MyBundle/Controller/Api/Rest/BaconController.php
class BaconController extends Controller implements ClassResourceInterface
{
/**
* @var Request $request
* @return array
* @Rest\View
*/
public function cgetAction(Request $request)
{
$mediaType = $request->attributes->get('media_type');
$format = $request->getFormat($mediaType);
my_dump($format);
return array(
array("id" => 1, "title" => "hello",),
array("id" => 2, "title" => "there",),
);
}
}
MyBundle/Resources/config/api/routing_rest.yml
my_api_rest_bacon:
type: rest
resource: "MyBundle:Api/Rest/Bacon"
name_prefix: api_rest_bacon_
prefix: /my/bacon
所以,此时 JSON 结果得到完美返回:
mysite.com/app_dev.php/api/my/bacon/bacons.json
返回我的数组。
但现在我需要让我的控制器生成包含数据的 PDF。所以我希望它在我调用时返回 PDF 文档:
mysite.com/app_dev.php/api/my/bacon/bacons.pdf
我找到了一些半手册:RSS view handler、RSS config.ynal、CSV issue with answers。并尝试制作类似的东西:
我已将这些行添加到
Symfony/app/config/config.yml
framework:
[...some old stuff here...]
request:
formats:
pdf: 'application/pdf'
fos_rest:
body_converter:
enabled: true
format_listener:
rules:
# Prototype array
-
# URL path info
path: ~
# URL host name
host: ~
prefer_extension: true
fallback_format: html
priorities: [html,json]
-
path: ~
host: ~
prefer_extension: true
fallback_format: pdf
priorities: [pdf]
view:
# @View or @Template
view_response_listener: force #true
formats:
json: true
pdf: true
xls: true
html: false
templating_formats:
pdf: false
xls: false
mime_types: {'pdf': ['application/pdf']}
routing_loader:
default_format: html
param_fetcher_listener: true
body_listener: true
allowed_methods_listener: true
services:
my.view_handler.pdf:
class: Lobster\MyBundle\View\PdfViewHandler
my.view_handler:
parent: fos_rest.view_handler.default
calls:
- ['registerHandler', [ 'pdf', [@my.view_handler.pdf, 'createResponse'] ] ]
MyBundle/View/PdfViewHandler.php
namespace Lobster\MyBundle\View;
use FOS\RestBundle\View\View;
use FOS\RestBundle\View\ViewHandler;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class PdfViewHandler
{
public function createResponse(ViewHandler $handler, View $view, Request $request, $format)
{
my_dump('pdf createResponse started');
$pdf = "some pdf";
return new Response($pdf, 200, $view->getHeaders());
}
}
所以现在当我打电话时
mysite.com/app_dev.php/api/my/bacon/bacons.pdf
我看到一个错误 An Exception was thrown while handling: Format html not supported, handler must be implemented 并且我的函数 my_dump 将有关文件格式的信息保存到文本文件中:它是 html,而不是 pdf。
pdf createResponse 也不起作用。为什么?
【问题讨论】:
标签: php symfony fosrestbundle