-
startup
Member
-
Posts: 55
Threads: 33
Joined: Jan 2022
Reputation:
0
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
-
kenjis
Administrator
-
Posts: 3,282
Threads: 90
Joined: Oct 2014
Reputation:
206
-
rmcdahal
Junior Member
-
Posts: 37
Threads: 11
Joined: Jan 2018
Reputation:
2
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();
}
});
-
datamweb
I'm interested in programming
-
Posts: 170
Threads: 10
Joined: Jun 2015
Reputation:
23
|