I've read the Bundle documentation (FOS rest-bundle), and can't find anything on compressing a response, and I'm unable to set compression to happen at the web-server level.
Is there a way to make the bundle return a gzip(or deflate) compressed response?
My current thinking it to implement a response listener, catch it and compress it but I feel like there's likely an existing way out there.
Edit
I was unable to find anything in FOS Rest Bundle that enabled this - most likely they expect it to be done at the server level. The solution was to create an Event Subscriber:
public function getSubscribedEvents() {
return [KernelEvents::RESPONSE => ['compressResponse', -256]];
}
Within my compress response method I'm using deflate on the body content and adding the proper content-encoding header:
public function compressResponse(FilterResponseEvent $event)
{
$response = $event->getResponse();
if ($response->headers->get('content-type') !== 'application/json') {
return;
}
$response->setContent(gzdeflate($response->getContent()));
$response->headers->set('Content-encoding', 'deflate');
}
This servers our purposes pretty well.