26

在 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 中完成这项工作。

或者

如果有的话,请提供一种在 laravel 5 中压缩 HTML 的更好方法。提前致谢。

注意:我不想使用任何 laravel 包来压缩 html,只需要一个简单的代码就可以完成工作而不会影响性能。

4

10 回答 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请在实施此代码之前/之后检查您的浏览器网络检查器的标头。

好好享受 ... :).. 。

于 2016-05-29T14:13:17.890 回答
21

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

相反,最好使用 htmlmin 包(https://github.com/HTMLMin/Laravel-HTMLMin):

composer require htmlmin/htmlmin
php artisan vendor:publish

在刀片模板级别缩小 HTML 并将其缓存在存储中应该更有效。

于 2017-08-17T10:45:41.617 回答
7

在 Larvel 5 中推荐的方法是将你的函数重写为中间件。如文档中所述:

..此中间件将在应用程序处理请求后执行其任务:

<?php namespace App\Http\Middleware;

class AfterMiddleware implements Middleware {

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

        // Perform action

        return $response;
    }
}
于 2015-03-23T04:52:14.193 回答
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 构建期间缩小所有视图文件。

于 2019-10-29T06:10:29.177 回答
2

这几乎是Vahid答案的副本,但它解决了两个问题。

1) 它检查响应是否为 a 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 的这个解决方案

于 2016-12-06T23:56:11.377 回答
1

以防万一您手动渲染视图:


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

于 2020-08-31T08:36:06.377 回答
0

在我看来,这个包是更好的选择renatomarinho/laravel-page-speed

于 2019-12-10T04:56:22.590 回答
0

我用非常简单的代码做到了。 示例:welcome.blade.php

将以下代码添加到页面开头

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

在页面末尾添加如下代码:</p>

<?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);
    }?>

于 2021-03-17T09:22:40.820 回答
-1

这是最好的方法..我们不需要使用 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;
    }
}

于 2017-01-04T18:02:57.427 回答
-3

为了方便压缩,我构建了自己的 laravel 模块。该模块将在发送到客户端(浏览器)之前压缩所有最终的 html 输出。

.env您还可以使用文件一次针对多个环境。

有关如何安装和配置的更多详细信息,请参见此处

于 2018-04-03T16:12:59.170 回答