How to compress the output of an HTML in Laravel?

3

I answered this question here once about the subject:

Store PHP script output in HTML "compacted" .

But now I would like to know how I can do this in Laravel.

When it comes to an HTML response, how could I capture all the rendering of the views, before being sent to the browser, and answering it compressed?

Where would I put this in Laravel 5?

In the answer above you already have the solution of how to compress, but I would like to know where I would do this action in Laravel, in the most appropriate way, to handle the content that goes to data output, before going to the client ( browser)?

    
asked by anonymous 14.09.2016 / 19:08

2 answers

3

Assuming you use Laravel 5.2 you can use the render method, then in Controller it would look something like, this is an example just to show how to use it, I changed the contents of $ data by a new output with return :

<?php

namespace App\Http\Controllers;

use App\User;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    public function home()
    {
        $v = view('welcome')->render(function ($obj, $data) {
            return 'Hello world!';
        });

        return $v;
    }
}

So just merge to the other link code getting something like:

function reduzirHtml($data)
{
    $search = array(
        '/\>[^\S ]+/s',  // strip whitespaces after tags, except space
        '/[^\S ]+\</s',  // strip whitespaces before tags, except space
        '/(\s)+/s'       // shorten multiple whitespace sequences
    );

    $replace = array(
        '>',
        '<',
        '\1'
    );

    return preg_replace($search, $replace, $data);
}

class UserController extends Controller
{
    public function home()
    {
        return view('welcome')->render(function ($obj, $data) {
             return reduzirHtml($data);
        });
    }

    public function about()
    {
        return view('welcome')->render(function ($obj, $data) {
             return reduzirHtml($data);
        });
    }
}

If you change the function to function reduzirHtml($obj, $data) you can call directly:

return view('welcome')->render("\App\Http\Controllers\reduzirHtml");

Or you can even move the reduzirHtml function to a file or method in a class and call it as in the example:

//Função
return view('welcome')->render("\reduzirHtml");

//Classe
return view('welcome')->render("\Namespace\Classe\reduzirHtml");
    
14.09.2016 / 19:34
1

To capture all HTML output from the application, you can also use a Middleware , which will preprocess this.

First run the command:

 php artisan make:middleware HtmlCompressor

Next, leave the handle method as in the example below:

namespace App\Http\Middleware;

use Closure;

/**
 * 
 * @author Wallace de Souza Vizerra <[email protected]>
 * 
 * */
class HtmlCompressor
{

    public function handle()
    {
        $response = $next($request);

        // Verifica se a saída é HTML

        if (str_contains($response->headers->get('content-type'), 'text/html')) {

            $search = [
                '/\>[^\S ]+/s',  // strip whitespaces after tags, except space
                '/[^\S ]+\</s',  // strip whitespaces before tags, except space
                '/(\s)+/s'       // shorten multiple whitespace sequences
            ];

            $replace = [
                '>',
                '<',
                '\1'
            ];

            $response->setContent(preg_replace($search, $replace, $response->getContent()));

            return $response;

        }

        return $response;
    }
}

In the App\Http\Kernel class of your application, you will add this middewlare that we create in the $middleware property:

protected $middleware = [
   // outros middlewares
   \App\Http\Middleware\HtmlCompressor
];
    
14.09.2016 / 20:29