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.
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.
We make that happen on Apache Level in order to enable webserver compression for application/json output as well by using the following conf.
Copied from standard deflate conf in PHP buildpack and overwriting it with:
<IfModule filter_module>
<IfModule deflate_module>
AddOutputFilterByType DEFLATE application/json text/html text/plain text/xml text/css text/javascript application/javascript
</IfModule>
</IfModule>
Adding application/json to this conf did the trick for us.