Laravel HTMLMin is currently maintained by Raza Mehdi, and is a simple HTML minifier for Laravel 5. It utilises Mr Clay's Minify package to minify entire responses, but can also minify blade at compile time. Feel free to check out the change log, releases, license, and contribution guidelines.
Laravel HTMLMin requires PHP 5.5+. supports all version of Laravel.
Step 1: Create Custom Validation
$ php artisan make:middleware OptimizeMiddleware
After above run command you will find one file on bellow location and you have to write following code:
<?php
/*
*
* (c) Farshad Ghanbari <eng.ghanbari2025@gmail.com>
*
*/
namespace App\Http\Middleware;
use Closure;
class OptimizeMiddleware
{
/**
* Handle an incoming request.
* (c) Farshad Ghanbari <eng.ghanbari2025@gmail.com>
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$response = $next($request);
$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);
ini_set('zlib.output_compression', 'On');
return $response;
}
}
After successfully write logic of middleware then we have to register this middleware on kernel file. So let's simply add this way:
<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array
*/
protected $middleware = [
....
\App\Http\Middleware\OptimizeMiddleware::class,
];
}
Laravel HTMLMin is licensed under The MIT License (MIT).