Shoora
5/21/2013 - 12:44 PM

HTML Output Minification in laravel 4

HTML Output Minification in laravel 4

<?php

### --- Snip --- ###

App::after(function($request, $response)
{
	// HTML Minification
	if(App::Environment() != 'local')
	{
		if($response instanceof Illuminate\Http\Response)
		{
			$output = $response->getOriginalContent();

			// Clean comments
			$output = preg_replace('/<!--([^\[|(<!)].*)/', '', $output);
			$output = preg_replace('/(?<!\S)\/\/\s*[^\r\n]*/', '', $output);

			// Clean Whitespace
			$output = preg_replace('/\s{2,}/', '', $output);
			$output = preg_replace('/(\r?\n)/', '', $output);

			$response->setContent($output);
		}
	}
});

### --- Snip --- ###