如何在Laravel 5中压缩HTML

27
在 Laravel 4.0 中,我使用以下代码来压缩 HTML laravel 的响应输出到浏览器,但是在 Laravel 5 中却无法生效。
App::after(function($request, $response)
{
    if($response instanceof Illuminate\Http\Response)
    {
        $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);
    }
});

请问如何在Laravel 5中使此代码生效。

或者,如果有更好的方法来压缩HTML,请提供。提前感谢。

注意:我不希望使用任何Laravel包来压缩HTML,只需要一个简单的代码即可完成工作,而不会影响性能。


1
我知道你说你不想要一个包,但是 https://github.com/GrahamCampbell/Laravel-HTMLMin 是完美的解决方案。它不会比你自己做还影响性能。 - Laurence
1
我强烈反对您尝试进行的操作 - pre元素并不是唯一需要考虑空格的地方,textarea/input中和任何元素内部(如果通过CSS进行格式化)都需要考虑。在将输出发送到客户端之前,只需使用 GZip 进行压缩,这比破坏HTML代码本身要有效得多。 - CBroe
@cbroe,我该如何使用GZip?有任何可用的示例吗? - Emeka Mbah
10个回答

25
完整的代码如下(启用自定义GZip):
<?php

namespace App\Http\Middleware;

use Closure;

class OptimizeMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @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'); // If you like to enable GZip, too!
        return $response;
    }
}

在实施此代码之前/之后,请检查浏览器网络检查器中的Content-Length头。

享受它... :).. .


(索引):1未捕获的语法错误:意外的输入JQuery在此之后不起作用 - Rahul Tathod
@Rahul Tathod 这个中间件只会压缩 HTML 并启用 GZip。这两种压缩不会影响 Jquery、CSS 或 JS 文件。如果出现任何问题,您应该找到根本原因。 - نرم افزار حضور و غیاب

22

在中间件中缩小HTML的解决方案并不是很好,因为它可能会花费大量CPU时间,并且它运行在每个请求上。

相反,更好的方法是使用htmlmin包(https://github.com/HTMLMin/Laravel-HTMLMin):

composer require htmlmin/htmlmin
php artisan vendor:publish

在Blade模板级别上压缩HTML并将其缓存到存储中应该更加有效。


如果模板中有需要在每个请求中考虑的变量,甚至是依赖于条件的整个块,这仍然有效吗? - Holonaut
2
2021年更新:这个软件包似乎有很多漏洞,而且似乎没有得到维护。 - ahinkle
不起作用,缺乏使用文档。这是我安装过的最糟糕的软件包之一,只用了10分钟就卸载了。 - Yusuf

7

在Laravel 5中,推荐的做法是将您的函数重写为中间件。如文档所述:

..该中间件会在请求被应用程序处理之后执行其任务:

<?php namespace App\Http\Middleware;

class AfterMiddleware implements Middleware {

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

        // Perform action

        return $response;
    }
}

好的。我该怎么使用它?我需要在每个请求上运行它吗? - Emeka Mbah
1
是的,您需要在每个HTTP请求上运行它。您可以通过将您的类添加到app/Http/Kernel.php中进行全局注册。 - darronz

4

我创建了一个Webpack插件来解决同样的目的。MinifyHtmlWebpackPlugin

使用npm安装该插件:

$ npm install minify-html-webpack-plugin --save-dev

Laravel Mix 用户使用说明

请将以下代码片段粘贴到 mix.js 文件中。

    const MinifyHtmlWebpackPlugin = require('minify-html-webpack-plugin');
    const mix = require('laravel-mix');

    mix.webpackConfig({
        plugins: [
            new MinifyHtmlWebpackPlugin({
                src: './storage/framework/views',
                ignoreFileNameRegex: /\.(gitignore)$/,
                rules: {
                    collapseWhitespace: true,
                    removeAttributeQuotes: true,
                    removeComments: true,
                    minifyJS: true,
                }
            })
        ]
    });

它将在Webpack构建期间压缩所有视图文件。

2

假设您正在手动渲染视图:


echo view('example.site')->render(function($view, $content) { 
    return preg_replace(
            ['/\>[^\S ]+/s', '/[^\S ]+\</s', '/(\s)+/s'],
            ['>', '<', '\\1'],
            $content
    ); }
);


2
这几乎是Vahid的答案的复制,但它解决了两个问题。
1)它检查响应是否为BinaryFileResponse,因为任何尝试修改此类型响应的操作都将引发异常。
2)它保留换行符,完全消除换行符会导致单行注释代码上出现错误的Javascript代码。
例如,下面的代码:
 var a; //This is a variable
 var b; //This will be commented out

将会变成

 var a; //This is a variable var b; //This will be commented out

注意:在回答时,我无法得到一个好的正则表达式来匹配单行注释而不带有复杂性,或者说只忽略带有单行注释的换行符,因此我希望能有更好的解决方法。
以下是修改后的版本。
<?php

namespace App\Http\Middleware;

use Closure;

class OptimizeMiddleware {

/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @return mixed
 */
public function handle($request, Closure $next)
{
    $response = $next($request);
    if ($response instanceof \Symfony\Component\HttpFoundation\BinaryFileResponse) {
        return $response;
    } else {
        $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+/" => "\n",
                "/\t/" => '',
                "/ +/" => ' ',
            );
        }
        $buffer = preg_replace(array_keys($replace), array_values($replace), $buffer);
        $response->setContent($buffer);
        ini_set('zlib.output_compression', 'On'); //enable GZip, too!
        return $response;
    }
  }
}

编辑

对于每个请求使用中间件压缩输出确实是一个不好的主意,我建议您查看Jokerius提出的这个解决方案


0

我用非常简单的代码实现了它。 示例:welcome.blade.php

在页面开头添加以下代码:

<?php ob_start('compress_page');?>

请在页面末尾添加以下代码:
<?php   
ob_end_flush();
function compress_page($buffer) {
    $search = array("/>[[:space:]]+/", "/[[:space:]]+</");
    $replace = array(">","<");
    return preg_replace($search, $replace, $buffer);
}?>

完整页面代码示例:

<?php ob_start('compress_page');?>
<!doctype html>
<html lang="{{ app()->getLocale() }}">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title>Laravel</title>

        <!-- Fonts -->
        <link href="https://fonts.googleapis.com/css?family=Raleway:100,600" rel="stylesheet" type="text/css">

        <!-- Styles -->
        <style>
            html, body {
                background-color: #fff;
                color: #636b6f;
                font-family: 'Raleway', sans-serif;
                font-weight: 100;
                height: 100vh;
                margin: 0;
            }
            .full-height {
                height: 100vh;
            }
            .flex-center {
                align-items: center;
                display: flex;
                justify-content: center;
            }
            .position-ref {
                position: relative;
            }
            .top-right {
                position: absolute;
                right: 10px;
                top: 18px;
            }
            .content {
                text-align: center;
            }
            .title {
                font-size: 84px;
            }
            .links > a {
                color: #636b6f;
                padding: 0 25px;
                font-size: 12px;
                font-weight: 600;
                letter-spacing: .1rem;
                text-decoration: none;
                text-transform: uppercase;
            }
            .m-b-md {
                margin-bottom: 30px;
            }
        </style>
    </head>
    <body>
        <div class="flex-center position-ref full-height">
            @if (Route::has('login'))
                <div class="top-right links">
                    @auth
                        <a href="{{ url('/home') }}">Home</a>
                    @else
                        <a href="{{ route('login') }}">Login</a>
                        <a href="{{ route('register') }}">Register</a>
                    @endauth
                </div>
            @endif

            <div class="content">
                <div class="title m-b-md">
                    Laravel
                </div>

                <div class="links">
                    <a href="https://laravel.com/docs">Documentation</a>
                    <a href="https://laracasts.com">Laracasts</a>
                    <a href="https://laravel-news.com">News</a>
                    <a href="https://forge.laravel.com">Forge</a>
                    <a href="https://github.com/laravel/laravel">GitHub</a>
                </div>
            </div>
        </div>
    </body>
</html>
<?php   
    ob_end_flush();
    function compress_page($buffer) {
        $search = array("/>[[:space:]]+/", "/[[:space:]]+</");
        $replace = array(">","<");
        return preg_replace($search, $replace, $buffer);
    }?>


0

-1

this is best way.. we don't need to use laravel packeges .Thanks..

<?php

namespace App\Http\Middleware;

use Closure;

class OptimizeMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @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'); // If you like to enable GZip, too!
        return $response;
    }
}


2
代码片段似乎无法运行。 - clearlight

-3
为了方便压缩,我构建了自己的 Laravel 模块。该模块将在发送到客户端(浏览器)之前压缩所有最终的 HTML 输出。
您还可以使用 .env 文件同时针对多个环境进行定位。
有关安装和配置的更多详细信息,请点击此处

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接