我正在研究两个不同的Symfony 2.8
项目运行在不同的服务器上。它想使用压缩来加快加载速度。我找到的所有资源都指向mod_deflate
。但是虽然第一台服务器不提供mod_deflate
根本,第二个服务器无法使用mod_deflate
while FastCGI
已启用。
我只找到了可以在服务器内启用压缩的信息(mod_deflate
)或“在脚本中”。但我没有找到有关此“脚本中”解决方案的任何详细信息。
以某种方式可以在 Symfony 中启用压缩而不使用mod_deflate
?
您可以尝试手动 gzip 内容kernel.response
event:
namespace AppBundle\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\HttpKernelInterface;
class CompressionListener implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return array(
KernelEvents::RESPONSE => array(array('onKernelResponse', -256))
);
}
public function onKernelResponse($event)
{
//return;
if ($event->getRequestType() != HttpKernelInterface::MASTER_REQUEST) {
return;
}
$request = $event->getRequest();
$response = $event->getResponse();
$encodings = $request->getEncodings();
if (in_array('gzip', $encodings) && function_exists('gzencode')) {
$content = gzencode($response->getContent());
$response->setContent($content);
$response->headers->set('Content-encoding', 'gzip');
} elseif (in_array('deflate', $encodings) && function_exists('gzdeflate')) {
$content = gzdeflate($response->getContent());
$response->setContent($content);
$response->headers->set('Content-encoding', 'deflate');
}
}
}
并在配置中注册这个监听器:
app.listener.compression:
class: AppBundle\EventListener\CompressionListener
arguments:
tags:
- { name: kernel.event_subscriber }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)