在没有mod_deflate的Symfony 2中使用gzip / compression

时间:2017-07-19 08:16:17

标签: apache symfony http-compression

我正在处理在不同服务器上运行的两个不同的Option Explicit Sub Charter() Dim MyCht As Object Dim my_range As Range Dim ws As Worksheet ' avoid using ActiveSheet, instead use fully qualifed objects Set ws = Worksheets("Sheet1") ' <-- change "Sheet1" to your sheet's name Set my_range = Selection ' set the Chart Set MyCht = ws.Shapes.AddChart2 With MyCht ' modify the chart's properties .Chart.ChartType = xlColumnStacked .Chart.SetSourceData Source:=my_range .Name = "My Chart" End With End Sub 项目。它希望使用压缩来加快加载速度。我找到的所有资源都指向Symfony 2.8。但是,当第一台服务器根本不提供mod_deflate时,第二台服务器在启用mod_deflate时无法使用mod_deflate

我只找到了可以在服务器(FastCGI)或脚本&#34;中启用压缩的信息。但我没有在脚本&#34;中找到任何有关此内容的详细信息。溶液

是否可以在不使用mod_deflate的情况下在Symfony中启用压缩?

1 个答案:

答案 0 :(得分:5)

您可以尝试在kernel.response事件中手动gzip内容:

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');
        }
    }
}

并在config:

中注册此侦听器
app.listener.compression:
    class: AppBundle\EventListener\CompressionListener
    arguments:
    tags:
        - { name: kernel.event_subscriber }