flightphp / core

An extensible micro-framework for PHP
https://docs.flightphp.com
MIT License
2.6k stars 407 forks source link

Minifier html #549

Closed oscarlosan closed 4 months ago

oscarlosan commented 4 months ago

Hello, before I used the following function to minify my html and it worked for me. With flightphp it does not minimize the html. Is there something stopping ob_start?

`<?php ob_start("minifier"); function minifier($code) { $search = array(

    // Remove whitespaces after tags
    '/\>[^\S ]+/s',

    // Remove whitespaces before tags
    '/[^\S ]+\</s',

    // Remove multiple whitespace sequences
    '/(\s)+/s',

    // Removes comments
    '/<!--(.|\s)*?-->/'
);
$replace = array('>', '<', '\\1');
$code = preg_replace($search, $replace, $code);
return $code;

}`

n0nag0n commented 4 months ago

With the 3.5 release, we had to overhaul how the output buffer works. You have a couple options, some simpler than others to get it working again.

  1. Flight::set('flight.v2.output_buffering', true); https://docs.flightphp.com/learn/migrating-to-v3 - This will implement the default behavior you're likely used to.
  2. All the output is now written to the Flight::response()->body property. Although as I write this out, there isn't a way to completely overwrite the body without calling Flight::response()->clear() and then running your minified function on the existing body content.

As a side note, I didn't see you close the output buffer, which might be part of the issue? Usually there's something like a ob_end_clean(); to properly close the output buffer (which was part of the problem that was addressed in the 3.5 update).

oscarlosan commented 4 months ago

Thank you very much for your support so quickly.

n0nag0n commented 3 months ago

FYI. Created this pull request which might be what you're after if you want to fully move to v3. https://github.com/flightphp/core/pull/571