vendredi 12 octobre 2018

Html minifier using Middleware in Laravel

I want to define a middleware named minifier to minify html for user on server, and use that for example:

Route::middleware('minifier')->view('welcome.blade.php');

the below code uses to minimize html:

function minifyHTML($htmlString)
{
    $replace = [
        '<!--(.*?)-->' => '', //remove comments
        "/<\?php/" => '<?php ',
        "/\n([\S])/" => '$1',
        "/\r/" => '', // remove carriage return
        "/\n/" => '', // remove new lines
        "/\t/" => '', // remove tab
        "/\s+/" => ' ', // remove spaces
    ];
    return preg_replace(array_keys($this->replace), array_values($this->replace), $htmlString);
}

I created a middleware but I don't know how can I use it.

 public function handle($request, Closure $next,$htmlString)
{
    $replace = [
        '<!--(.*?)-->' => '', //remove comments
        "/<\?php/" => '<?php ',
        "/\n([\S])/" => '$1',
        "/\r/" => '', // remove carriage return
        "/\n/" => '', // remove new lines
        "/\t/" => '', // remove tab
        "/\s+/" => ' ', // remove spaces
    ];
    return preg_replace(array_keys($this->replace), array_values($this->replace), $htmlString);
}

assume this is true, how can I use to minify Html?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire