Skip to content

Latest commit

 

History

History
107 lines (90 loc) · 3.41 KB

README.md

File metadata and controls

107 lines (90 loc) · 3.41 KB

Laravel HTMLMin

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.

StyleCI Status Software License Latest Version

This package isn’t work on Laravel 5.6 version, for use in laravel 5.6 follow the tutorial :

Installation

Laravel HTMLMin requires PHP 5.5+. supports all version of Laravel.

Configuration

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:

app/Http/Middleware/OptimizeMiddleware.php
<?php

/*
 *
 * (c) Farshad Ghanbari <[email protected]>
 *
 */

namespace App\Http\Middleware;

use Closure;

class OptimizeMiddleware
{
    /**
     * Handle an incoming request.
     * (c) Farshad Ghanbari <[email protected]>
     * @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:

app/Http/Kernel.php
<?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,
    ];
}

License

Laravel HTMLMin is licensed under The MIT License (MIT).