-
-
Save rgomezcasas/6786469 to your computer and use it in GitHub Desktop.
Minificar salida del HTML en Laravel 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
App::after(function($request, $response) | |
{ | |
// Elimina esta condición si quieres probarlo en local | |
if (App::Environment() == 'production') | |
{ | |
if ($response instanceof Illuminate\Http\Response) | |
{ | |
$output = $response->getOriginalContent(); | |
$filters = array( | |
'/<!--([^\[|(<!)].*)/' => '', // Eliminamos los comentarios HTML | |
'/<(?<!\S)\/\/\s*[^\r\n]*/' => '', // Borra los comentarios /* */ | |
'/<\s{2,}/' => '', // Combina los espacios | |
'/<(\r?\n)/' => '', // Elimina los saltos de linea | |
); | |
$output = preg_replace(array_keys($filters), array_values($filters), $output); | |
$response->setContent($output); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment