Aji*_*ari 1 codeigniter codeigniter-4
我知道我们可以通过钩子在 CI3 中缩小 HTML,但在 CI 4 中
我通过在每个方法的返回之前添加一个 minify 函数来完成此操作。
minifyHTML(view('admin/template/template',$this->data));
Run Code Online (Sandbox Code Playgroud)
有什么其他方法可以在不到处使用 minify 函数的情况下做到这一点吗?
我还找到了另一个解决方案,即在 BaseConroller 中添加一个模板函数来呈现所有视图。但我已经在项目的很多地方使用了 view() ,它对我来说不可行,但对其他人来说可以。
在 CodeIgniter4 中,您可以使用事件来缩小输出(而不是像我们在 CodeIgniter3 中那样使用 Hooks),方法是在 intiapp/Config/Events.php文件中添加以下代码
//minify html output on codeigniter 4 in production environment
Events::on('post_controller_constructor', function () {
if (ENVIRONMENT !== 'testing') {
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;
});
}
});
Run Code Online (Sandbox Code Playgroud)
请参阅https://gitlab.irbidnet.com/-/snippets/3
| 归档时间: |
|
| 查看次数: |
2480 次 |
| 最近记录: |