Instantly share code, notes, and snippets.

Embed
What would you like to do?
Laravel Filter Helper - Compress HTML Output & Strip Comments
<?php
### --- Snip --- ###
App::after(function($request, $response)
{
// HTML Minification
if(App::Environment() != 'local')
{
if($response instanceof Illuminate\Http\Response)
{
$output = $response->getOriginalContent();
$filters = array(
'/<!--([^\[|(<!)].*)/' => '', // Remove HTML Comments (breaks with HTML5 Boilerplate)
'/(?<!\S)\/\/\s*[^\r\n]*/' => '', // Remove comments in the form /* */
'/\s{2,}/' => ' ', // Shorten multiple white spaces
'/(\r?\n)/' => '', // Collapse new lines
);
$output = preg_replace(array_keys($filters), array_values($filters), $output);
$response->setContent($output);
}
}
});
### --- Snip --- ###
@RodrigoEspinosa

This comment has been minimized.

Show comment
Hide comment
@RodrigoEspinosa

RodrigoEspinosa Oct 8, 2013

Really nice! I think the HTML Comments migth be neccesary either way.

RodrigoEspinosa commented Oct 8, 2013

Really nice! I think the HTML Comments migth be neccesary either way.

@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost May 22, 2014

Thanks for the alteration that allows for HTML comments.

One issue: Line 17 (shorten multiple white spaces) should be reduced to a space rather than nothing. The problem that I had with this was I had

 class="classOne  classTwo" //two spaces

and the white space reducer was reducing it down to

 class="classOneclassTwo"

thus breaking my theme.

ghost commented May 22, 2014

Thanks for the alteration that allows for HTML comments.

One issue: Line 17 (shorten multiple white spaces) should be reduced to a space rather than nothing. The problem that I had with this was I had

 class="classOne  classTwo" //two spaces

and the white space reducer was reducing it down to

 class="classOneclassTwo"

thus breaking my theme.

@AucT

This comment has been minimized.

Show comment
Hide comment
@AucT

AucT Jun 21, 2014

thx for this

AucT commented Jun 21, 2014

thx for this

@garagesocial

This comment has been minimized.

Show comment
Hide comment
@garagesocial

garagesocial Oct 14, 2014

@RodrigoEspinosa we prefer to not use HTML comments especially since Laravel ships with blade you can just use blade comments that won't show up in the output {{-- My Coment --}}

@tylerssn are you sure you are having this problem? It's currently replacing it with one space like you suggest '/\s{2,}/' => ' ',

@AucT welcome and also thanks to the original contributor

Owner

garagesocial commented Oct 14, 2014

@RodrigoEspinosa we prefer to not use HTML comments especially since Laravel ships with blade you can just use blade comments that won't show up in the output {{-- My Coment --}}

@tylerssn are you sure you are having this problem? It's currently replacing it with one space like you suggest '/\s{2,}/' => ' ',

@AucT welcome and also thanks to the original contributor

@rotaercz

This comment has been minimized.

Show comment
Hide comment
@rotaercz

rotaercz Mar 20, 2015

Learning Laravel. Where do you put this to make it work?

rotaercz commented Mar 20, 2015

Learning Laravel. Where do you put this to make it work?

@scoutkirkolson

This comment has been minimized.

Show comment
Hide comment
@scoutkirkolson

scoutkirkolson Sep 1, 2016

Same question here:
Learning Laravel. Where do you put this to make it work?

scoutkirkolson commented Sep 1, 2016

Same question here:
Learning Laravel. Where do you put this to make it work?

@nuqz

This comment has been minimized.

Show comment
Hide comment
@nuqz

nuqz Mar 13, 2017

@rotaercz @KirkOlson
In Laravel 4.x you should put it into app\filters.php

In modern Laravel 5.x according to the docs you should put it into middleware (see "Before & After Middleware" section).

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

        // HTML Minification
        ....................

        return $response;
}

nuqz commented Mar 13, 2017

@rotaercz @KirkOlson
In Laravel 4.x you should put it into app\filters.php

In modern Laravel 5.x according to the docs you should put it into middleware (see "Before & After Middleware" section).

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

        // HTML Minification
        ....................

        return $response;
}
@webciter

This comment has been minimized.

Show comment
Hide comment
@webciter

webciter Aug 13, 2018

Full Laravel 5.x Middleware to add to your projects, https://github.com/webciter/RemoveExcessWhitespaceMiddleware

webciter commented Aug 13, 2018

Full Laravel 5.x Middleware to add to your projects, https://github.com/webciter/RemoveExcessWhitespaceMiddleware

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment