PHP slim framework middleware to minify HTML output

Overview

slim-minify

Slim middleware to minify HTML output generated by the slim PHP framework. It removes whitespaces, empty lines, tabs beetween html-tags and comments to reduce traffic. This script is a summary of stackoverflow answers.

Requirements

  • PHP Version >= 7.2
  • Slim Version >= 4.0

Usage

Copy the file Minify.php to 'Slim/Middleware/'. Register minify via $app->add():

or use the composer:

    "require": {
        "christianklisch/slim-minify": "0.7.0"
    }

in 'src/middleware.php':

$app->add(new \Slim\Middleware\Minify() );

to deactivate minified output:

$app->add(new \Slim\Middleware\Minify(false);

Contributors

Copyright and license

Copyright 2014 released under MIT license.

Comments
  • Support slim v4

    Support slim v4

    $app = AppFactory::createFromContainer($container);
    $app->add(new \Slim\Middleware\Minify());
    

    Fatal error: Uncaught TypeError: Argument 2 passed to Slim\Middleware\Minify::__invoke() must be an instance of Psr\Http\Message\ResponseInterface, instance of Slim\Routing\RouteRunner given, called in /home/site/vendor/slim/slim/Slim/MiddlewareDispatcher.php on line 313 and defined in /home/site/vendor/christianklisch/slim-minify/src/Slim/Middleware/Minify.php:61 Stack trace: #0 /home/site/vendor/slim/slim/Slim/MiddlewareDispatcher.php(313): Slim\Middleware\Minify->__invoke(Object(Slim\Psr7\Request), Object(Slim\Routing\RouteRunner)) #1 /home/site/vendor/slim/slim/Slim/MiddlewareDispatcher.php(81): class@anonymous->handle(Object(Slim\Psr7\Request)) #2 /home/site/vendor/slim/slim/Slim/App.php(215): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request)) #3 /home/site/vendor/slim/slim/Slim/App.php(199): Slim\App->handle(Object(Slim\Psr7\Request)) #4 /home/site/public/index.php(138): Slim\App->run() #5 {main} thrown in /home/site/vendor/christianklisch/slim-minify/src/Slim/Middleware/Minify.php on line 61

    image

    opened by Jebati 2
  • Compatible with Slim 3 ?

    Compatible with Slim 3 ?

    Does this middleware work with Slim 3? I get this error:

    Fatal error: Class 'Slim\Middleware' not found in /home/pierre/www/alltube/vendor/christianklisch/slim-minify/src/Slim/Middleware/Minify.php on line 34
    
    opened by Rudloff 2
  • Redirects return blank page with HTTP 200 status

    Redirects return blank page with HTTP 200 status

    I couldn't perform redirects: return $response->withRedirect('/page'); I'd get a blank page with an HTTP 200 status code.

    I was able to fix this on my machine by changing $next($request,$response); to $response = $next($request,$response);

    (Since the Response is immutable as per the user guide.)

    opened by zimozy 1
  • Fixed regex to not replace protocol less urls preceeded by a double quote

    Fixed regex to not replace protocol less urls preceeded by a double quote

    Fixed an issue where having protocol less urls on page would break minification.

    Ex: <script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.13.0/jquery.validate.min.js"></script>

    opened by kdunlap 1
  • Support for JavaScript Comments

    Support for JavaScript Comments

    Support for removal of JavaScript comments, since many inline script snippets are often present in templates. Without this a section like

    <script>
    console.log(1);
    //console.log(2);
    console.log(3);
    </script>
    

    changes to <script>console.log(1);//console.log(2);console.log(3);</script> thus effectively commenting out the third console.log() As a solution, I have removed the commented part in output file, which also helps in the cause of minification.

    opened by sankalpsans 1
  • Fix error

    Fix error "variable body is not defined"

    While doing preg_replace, the variable $body was missing because it was named $squeezedHTML. Now it is renamed to $body and getting "undefined variable" error anymore.

    opened by bbarreto 1
  • Add feature switch

    Add feature switch

    to configure it easily with multiple environment (and to turn this off in development), i added this handy non-obtrusive switch.

    default is active of course.

    opened by scones 0
  • problem with images.

    problem with images.

    there's a problem when the response is an image, it's not possible to use with some libraries, for example:

    this is the code that I used: return $response->withHeader('Content-type', 'image/jpeg')->withBody($image->stream('jpg', 80));

    opened by ninjanero 0
  • asynchronous adsense format problem

    asynchronous adsense format problem

    cannot render asynchronous adsense format :

    <* script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></* script>

    < ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-xxx" data-ad-slot="xxx" data-ad-format="auto"></ ins> <* script> (adsbygoogle = window.adsbygoogle || []).push({}); </* script>

    if minify was activate, adsense just displayed as

    </_ins><_script>(adsbygoogle = window.adsbygoogle || []).push({});

    can you fix this? i realy like your slim minify :+1:

    bug 
    opened by dianariyanto 1
Releases(0.7.0)
Owner
Christian Klisch
Christian Klisch
This repository contains a library of optional middleware for your Slim Framework application

Slim Framework Middleware This repository contains a library of optional middleware for your Slim Framework application. How to Install Update your co

Slim Framework 47 Nov 7, 2022
Juliangut Slim Framework Doctrine handler middleware

Juliangut Slim Framework Doctrine handler middleware Doctrine handler middleware for Slim Framework. Slim3 version Doctrine integration service for Sl

Julián Gutiérrez 6 Mar 23, 2021
This Slim Framework middleware will compile LESS CSS files on-the-fly using the Assetic library

This Slim Framework middleware will compile LESS CSS files on-the-fly using the Assetic library. It supports minification and caching, also via Asseti

Gerard Sychay 22 Mar 31, 2020
Access control middleware for Slim framework

Slim Access Access control middleware for Slim framework. Supported formats IPv4 and IPv6 addresses CIDR notation all keyword Installation composer re

Alexandre Bouvier 7 Oct 22, 2019
Redis cache middleware for Slim framework

RedisCache Redis cache middleware for Slim framework. Installation composer require abouvier/slim-redis-cache Usage Cache every successful HTTP respo

Alexandre Bouvier 16 Sep 20, 2021
Slim Framework HTTP cache middleware and service provider

Slim Framework HTTP Cache This repository contains a Slim Framework HTTP cache middleware and service provider. Install Via Composer $ composer requir

Slim Framework 107 Dec 15, 2022
Slim Framework CSRF protection middleware

Slim Framework CSRF Protection This repository contains a Slim Framework CSRF protection PSR-15 middleware. CSRF protection applies to all unsafe HTTP

Slim Framework 302 Dec 11, 2022
Simple RBAC Manager for Yii2 (minify of yii2-admin)

Yii2 Mimin Simple RBAC Manager fo Yii 2.0. Minify of yii2-admin extension with awesome features Attention Before you install and use this extension, t

Hafid Mukhlasin 52 Sep 22, 2022
Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.

Slim Framework Slim is a PHP micro-framework that helps you quickly write simple yet powerful web applications and APIs. Installation It's recommended

Slim Framework 11.5k Jan 4, 2023
A Slim PHP MVC framework built just for fun!

Aura Framework A Slim PHP MVC framework built just for fun! en: Note: This repository only contains the core code of the Aura framework. If you want t

Murilo Magalhães Barreto 2 Dec 16, 2021
The Slim PHP micro framework paired with Laravel's Illuminate Database toolkit.

Slim & Eloquent The Slim PHP micro framework paired with Laravel's Illuminate Database toolkit. Getting started # Download composer curl -s https://ge

Kyle Ladd 111 Jul 23, 2022
PHP boilerplate for quick start projects using Slim Framework and Eloquent.

Lassi PHP boilerplate for quick projects using Slim Framework and Eloquent database. Lassi is a small PHP boilerplate to use Slim Framework with Eloqu

Jabran Rafique⚡️ 59 Jul 14, 2022
Slim Framework - Prerequisite Checker

Slim Framework - Server Configuration Checker Upload the file check.php to your webserver Browse to the file: https://example.com/check.php Check the

Daniel Opitz 6 Aug 30, 2022
REST APIs using Slim framework. Implemented all CRUD operations on the MySql database

PHP REST API using slim framework and CRUD operations ?? Hi there, this is a simple REST API built using the Slim framework. And this is for the folks

Hanoak 2 Jun 1, 2022
Slim Framework skeleton application with MVC Schema

Slim Framework skeleton application with MVC Schema

JingwenTian 9 Apr 29, 2021
A curated list of awesome tutorials and other resources for the Slim micro framework

Awesome Slim A curated list of awesome tutorials and other resources for the Slim micro framework Table of Contents Essentials Tutorials Packages and

Sawyer Charles 466 Dec 8, 2022
Slim Framework 3 Skeleton Application + PagSeguro Lib

Slim Framework 3 Skeleton Application + PagSeguro Lib Aplicação simples para geração do Token para pagamentos no PagSeguro (método transparente) e env

Raí Siqueira 1 Feb 26, 2018
My personal blog developed on the Slim Framework

nesbot.com I am making the source code of my personal site available publicly in case it helps anybody. It's developed using the Slim Framework. I am

Brian Nesbitt 56 Sep 14, 2022
Plates Template Integration for Slim micro framework 3

Plates Template Integration for Slim micro framework 3 Render your Slim 3 application views using Plates template engine. Install Via Composer $ compo

Projek XYZ 26 Feb 5, 2022