- Notifications
You must be signed in to change notification settings - Fork0
A simple Middleware HTML minifier for Laravel
License
FarshadGhanbari/laravel-HTMLMin
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Laravel HTMLMin is currently maintained byRaza Mehdi, and is a simple HTML minifier forLaravel 5. It utilises Mr Clay'sMinify package to minify entire responses, but can also minify blade at compile time. Feel free to check out thechange log,releases,license, andcontribution guidelines.
Laravel HTMLMin requiresPHP 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*/ publicfunctionhandle($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:
<?phpnamespace 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 underThe MIT License (MIT).
About
A simple Middleware HTML minifier for Laravel
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.