CodeIgniter Forums
CI 4 Output Compression - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: CI 4 Output Compression (/showthread.php?tid=79455)



CI 4 Output Compression - [email protected] - 06-18-2021

Hi..


Anyone can help how to set output compression on CI4?


I read about content negotiation but no luck to set the output.


On CI 3 i just change to this line on config.php

$config['compress_output'] = TRUE;

Thanks..


RE: CI 4 Output Compression - InsiteFX - 06-19-2021

READ:
CodeIgniter 4 User Guide - Encoding


RE: CI 4 Output Compression - [email protected] - 06-19-2021

(06-19-2021, 12:51 AM)InsiteFX Wrote: READ:
CodeIgniter 4 User Guide - Encoding

Thanks, i am new on web programming. I have read that but still not sure how to make it works. I've tried this code on controller :

$negotiate = \Config\Services::negotiator();
$type = $negotiate->encoding(['gzip']);

But the output still not on gzip encoding.
I am using ajax call to controller and check the content encoding on firefox with inspect.


RE: CI 4 Output Compression - InsiteFX - 06-19-2021

If I remember correctly there is a php.ini setting for it. Also you need to check your hosting provider
to see what types of encoding they allow. Check php.ini for mod_deflate.

Code:
# BEGIN GZIP
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
</ifmodule>
# END GZIP


This is for .htaccess GZip.


RE: CI 4 Output Compression - [email protected] - 06-19-2021

Thanks for replying,
The server support gzip. Client browser also. Before i was doing with setting config.php output compression = true on CI3, so i was hoping if there is a way to set that on CI4 so that i dont have to modify htaccess or php.ini
I was saving for last if there is no way to set through CI4.
I'm on process on learning how to compress just the ajax response data.


RE: CI 4 Output Compression - stopz - 09-02-2021

In your Controller you can call for negotiator service to set gzip encoding.
PHP Code:
service('negotiator')->encoding(['gzip']); 

In your public/.htaccess you may define other resources by file-extension to be gzip encoded by apache.
Code:
<IfModule mod_deflate.c>
<FilesMatch "\.(js|css|html|htm|php|json|xml|jpg|jpeg|gif|raw)$">
SetOutputFilter DEFLATE
</FilesMatch>
</IfModule>

For apache gzip encoding to work: make sure you have this line uncommented in your apache/conf/httpd.conf
Code:
LoadModule deflate_module modules/mod_deflate.so

Let me know if it worked for you Smile


RE: CI 4 Output Compression - nc03061981 - 12-10-2021

(09-02-2021, 12:19 AM)stopz Wrote: In your Controller you can call for negotiator service to set gzip encoding.
PHP Code:
service('negotiator')->encoding(['gzip']); 

In your public/.htaccess you may define other resources by file-extension to be gzip encoded by apache.
Code:
<IfModule mod_deflate.c>
<FilesMatch "\.(js|css|html|htm|php|json|xml|jpg|jpeg|gif|raw)$">
SetOutputFilter DEFLATE
</FilesMatch>
</IfModule>

For apache gzip encoding to work: make sure you have this line uncommented in your apache/conf/httpd.conf
Code:
LoadModule deflate_module modules/mod_deflate.so

Let me know if it worked for you Smile

Thanks, it worked for me!!!


RE: CI 4 Output Compression - [email protected] - 12-12-2021

(09-02-2021, 12:19 AM)stopz Wrote: In your Controller you can call for negotiator service to set gzip encoding.
PHP Code:
service('negotiator')->encoding(['gzip']); 

In my case i don't have to set this in controller. Just with the .htaccess and mod_deflate.so enable to make it work.
Thanks.
But still is there no other way to set compression without changing apache module?
I found a way to force gzip/compression without changing apache module but not sure if it's a good practice.
I edit Events.php file in config like this :

Code:
if(!ob_start("ob_gzhandler")){
if (ini_get('zlib.output_compression'))
{
throw FrameworkException::forEnabledZlibOutputCompression();
}

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

ob_start(function ($buffer) {
return $buffer;
});
}