FOS Rest Bundle:可以对响应进行 gzip 压缩吗?
FOS Rest Bundle: Can the response be gzipped?
我已阅读 Bundle 文档 (FOS rest-bundle),但找不到有关压缩响应的任何内容,而且我无法将压缩设置为 web-server 级别.
有没有办法使包 return 成为 gzip(或 deflate)压缩响应?
我目前的想法是实现一个响应监听器,捕获它并压缩它,但我觉得可能有一个现有的方法。
编辑
我无法在 FOS Rest Bundle 中找到启用此功能的任何内容 - 很可能他们希望它在服务器级别完成。解决方案是创建事件订阅者:
public function getSubscribedEvents() {
return [KernelEvents::RESPONSE => ['compressResponse', -256]];
}
在我的压缩响应方法中,我对 body 内容使用 deflate 并添加适当的 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');
}
这很好地服务于我们的目的。
我们在 Apache 级别实现了这一点,以便通过使用以下配置为 application/json 输出启用网络服务器压缩。
从 standard deflate conf in PHP buildpack 复制并覆盖为:
<IfModule filter_module>
<IfModule deflate_module>
AddOutputFilterByType DEFLATE application/json text/html text/plain text/xml text/css text/javascript application/javascript
</IfModule>
</IfModule>
将 application/json 添加到此 conf 对我们有用。
我已阅读 Bundle 文档 (FOS rest-bundle),但找不到有关压缩响应的任何内容,而且我无法将压缩设置为 web-server 级别.
有没有办法使包 return 成为 gzip(或 deflate)压缩响应?
我目前的想法是实现一个响应监听器,捕获它并压缩它,但我觉得可能有一个现有的方法。
编辑
我无法在 FOS Rest Bundle 中找到启用此功能的任何内容 - 很可能他们希望它在服务器级别完成。解决方案是创建事件订阅者:
public function getSubscribedEvents() {
return [KernelEvents::RESPONSE => ['compressResponse', -256]];
}
在我的压缩响应方法中,我对 body 内容使用 deflate 并添加适当的 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');
}
这很好地服务于我们的目的。
我们在 Apache 级别实现了这一点,以便通过使用以下配置为 application/json 输出启用网络服务器压缩。
从 standard deflate conf in PHP buildpack 复制并覆盖为:
<IfModule filter_module>
<IfModule deflate_module>
AddOutputFilterByType DEFLATE application/json text/html text/plain text/xml text/css text/javascript application/javascript
</IfModule>
</IfModule>
将 application/json 添加到此 conf 对我们有用。