0

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?

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
Ahmad
  • 73
  • 3
  • 13

2 Answers2

2

I have a public package for Laravel doing exactly this, but let's assume we are using your code... :-)

But your code is not correct, as another answer here pointed out. You need to call the Closure as well.

So first make sure you change the content of your handle method. And after that let's focus on your question: how to use your code... ;-)

This is how you create middleware in Laravel.

First create a middleware class by using artisan itself...

php artisan make:middleware MinifyHtml

A class is created for you in the correct place. Put your handle method in that class. Add the class to the kernel.php

protected $middleware = [
    ...
    MinifyHtml::class,
    ...
];

And the middleware is being used, as you asked... ;-)

About your handle method

public function handle(Request $request, Closure $next) {

    $response = $next($request);
    $content = $response->getContent();
    $output = .... your code ....
    $response->setContent($output);
    return $response;
}

Explanation:

  • First call the downstream code to get the response that you need to minify
  • Then get the content from that response
  • minify that content
  • put the minified content back in the response
  • return the respons

BTW this is pseudo code, so you need to tweak it a bit to wotk, but it will give you a general idea how to proceed

redcenter
  • 826
  • 4
  • 10
1

You should define method handle like this:

public function handle($request, Closure $next, $guard = null)
{
    // get response
    $response = $next($request);

    // get content (I assume you use only HTML view)
    $content = $response->getContent();

    // here you use your algorithm to modify content
    $modifiedContent = $this->minifyHTML($content)

    // here you set modified content for response
    $response->setContent($modifiedContent);

    // finally you return response with modified content
    return $response;
}
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291