php - Symfony2 FOSRESTBundle REST API to return PDF -
i've made bundle , rest controller inside. "index" method return array in json-format, it's ok:
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
so, @ point json results returned perfectly:
mysite.com/app_dev.php/api/my/bacon/bacons.json
returns array.
but need controller generate pdf data. want return pdf document when call:
mysite.com/app_dev.php/api/my/bacon/bacons.pdf
i've found half-manuals: rss view handler, rss config.ynal, csv issue answers. , tried make similar:
i've added these lines to
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()); } }
so when call
mysite.com/app_dev.php/api/my/bacon/bacons.pdf
i see error an exception thrown while handling: format html not supported, handler must implemented
, function my_dump
saves text file info file format: html
, not pdf
.
also pdf createresponse
didn't work. why?
so i've found solution (i describe how enable 2 output formats: pdf , xls):
1) section in config.yml
not needed:
framework: [...some old stuff here...] request: formats: pdf: 'application/pdf'
2) fos_rest.format_listener
section in config.yml
should this:
format_listener: rules: - path: '^/api/my/bacon.*\.xls$' host: ~ prefer_extension: false fallback_format: json priorities: [xls, json] - path: '^/api/my/bacon.*\.pdf$' host: ~ prefer_extension: false fallback_format: json priorities: [pdf, json] - path: ~ host: ~ prefer_extension: true fallback_format: html priorities: [html,json]
3) need add service
section fos_rest
in config.yml
fos_rest: [...] service: view_handler: my.view_handler
4) services
root section in config.yml
should like
services: my.view_handler.xls: class: lobster\mybundle\view\xlsviewhandler my.view_handler.pdf: class: lobster\mybundle\view\pdfviewhandler my.view_handler: parent: fos_rest.view_handler.default calls: - ['registerhandler', ['xls', [@my.view_handler.xls, 'createresponse'] ] ] - ['registerhandler', ['pdf', [@my.view_handler.pdf, 'createresponse'] ] ]
and it. works perfect
Comments
Post a Comment