CodeIgniter Forums
plz, show me to run minify in codeigniter 4 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: plz, show me to run minify in codeigniter 4 (/showthread.php?tid=84941)



plz, show me to run minify in codeigniter 4 - startup - 11-24-2022

i read  to minify codeigniter 4 on https://gitlab.irbidnet.com/-/snippets/3 , but i dont understand  and dont work
go to app/Config/Events.php
PHP Code:
//minify html output on codeigniter 4
if (ENVIRONMENT !== 'development')
    {
    Events::on('post_controller_constructor', function () {


        while (ob_get_level() > 0)
        {
            ob_end_flush();
        }

        ob_start(function ($buffer) {
            $search = array(
                '/\n/',      // replace end of line by a <del>space</del> nothing , if you want space make it down ' ' instead of ''
                '/\>[^\S ]+/s',    // strip whitespaces after tags, except space
                '/[^\S ]+\</s',    // strip whitespaces before tags, except space
                '/(\s)+/s',    // shorten multiple whitespace sequences
                '/<!--(.|\s)*?-->/' //remove HTML comments
            );

            $replace = array(
                '',
                '>',
                '<',
                '\\1',
                ''
            );

            $buffer preg_replace($search$replace$buffer);
            return $buffer;
        }); 

can someone demo this code?
thank you very much


RE: plz, show me to run minify in codeigniter 4 - kenjis - 11-24-2022

It does not seem the code works.

It is better to port the CI3 code to CI4 Controller Filter.
https://thedebuggers.com/minify-html-codeigniter-using-hooks/

See https://codeigniter4.github.io/CodeIgniter4/incoming/filters.html#after-filters


RE: plz, show me to run minify in codeigniter 4 - rmcdahal - 11-25-2022

This is working for HTML output minification in the production environment.

Code:
<?php

namespace Config;

use CodeIgniter\Events\Events;
use CodeIgniter\Exceptions\FrameworkException;

/*
* --------------------------------------------------------------------
* Application Events
* --------------------------------------------------------------------
* Events allow you to tap into the execution of the program without
* modifying or extending core files. This file provides a central
* location to define your events, though they can always be added
* at run-time, also, if needed.
*
* You create code that can execute by subscribing to events with
* the 'on()' method. This accepts any form of callable, including
* Closures, that will be executed when the event is triggered.
*
* Example:
*      Events::on('create', [$myInstance, 'myMethod']);
*/

Events::on('pre_system', static function () {
    if (ENVIRONMENT !== 'testing') {
        if (ini_get('zlib.output_compression')) {
            throw FrameworkException::forEnabledZlibOutputCompression();
        }

        while (ob_get_level() > 0) {
            ob_end_flush();
        }

        ob_start(static fn ($buffer) => $buffer);
    }
//minify html output on codeigniter 4
if (ENVIRONMENT !== 'development')
    {
    Events::on('post_controller_constructor', function () {


        while (ob_get_level() > 0)
        {
            ob_end_flush();
        }

        ob_start(function ($buffer) {
            $search = array(
                '/\n/',      // replace end of line by a <del>space</del> nothing , if you want space make it down ' ' instead of ''
                '/\>[^\S ]+/s',    // strip whitespaces after tags, except space
                '/[^\S ]+\</s',    // strip whitespaces before tags, except space
                '/(\s)+/s',    // shorten multiple whitespace sequences
                '/<!--(.|\s)*?-->/' //remove HTML comments
            );

            $replace = array(
                '',
                '>',
                '<',
                '\\1',
                ''
            );

            $buffer = preg_replace($search, $replace, $buffer);
            return $buffer;
        });
    });
};

    /*
    * --------------------------------------------------------------------
    * Debug Toolbar Listeners.
    * --------------------------------------------------------------------
    * If you delete, they will no longer be collected.
    */
    if (CI_DEBUG && ! is_cli()) {
        Events::on('DBQuery', 'CodeIgniter\Debug\Toolbar\Collectors\Database::collect');
        Services::toolbar()->respond();
    }
});



RE: plz, show me to run minify in codeigniter 4 - datamweb - 11-25-2022

Hello,
See https://github.com/michalsn/minifier


RE: plz, show me to run minify in codeigniter 4 - startup - 11-25-2022

thank you all, today i test again, it's working, thank you for  helping