In Laravel 4.0, I use the code below to compress the HTML laravel response outputs to browser, however it doesn't work in laravel 5.
App::after(function($request, $response)
{
if($response instanceof Illuminate\Http\Response)
{
$buffer = $response->getContent();
if(strpos($buffer,'<pre>') !== false)
{
$replace = array(
'/<!--[^\[](.*?)[^\]]-->/s' => '',
"/<\?php/" => '<?php ',
"/\r/" => '',
"/>\n</" => '><',
"/>\s+\n</" => '><',
"/>\n\s+</" => '><',
);
}
else
{
$replace = array(
'/<!--[^\[](.*?)[^\]]-->/s' => '',
"/<\?php/" => '<?php ',
"/\n([\S])/" => '$1',
"/\r/" => '',
"/\n/" => '',
"/\t/" => '',
"/ +/" => ' ',
);
}
$buffer = preg_replace(array_keys($replace), array_values($replace), $buffer);
$response->setContent($buffer);
}
});
Please how do i make this work in Laravel 5.
OR
Please provide a better way of compressing HTML in laravel 5 if any. Thanks in advance.
NB: I don't wish to use any laravel package for compressing html, just need a simple code that does the work without killing performance.
pre
element is not the only thing where white space might matter, but also inside atextarea
/input
or basically in any element if it gets later on formatted via CSS (white-space
). Just GZip the output before sending it to the client, that is much more effective than messing with the HTML code itself. – Oleta