Welcome Guest, Not a member yet? Register   Sign In
CI 4 Output Compression
#1

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..
Reply
#2

READ:
CodeIgniter 4 User Guide - Encoding
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(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.
Reply
#4

(This post was last modified: 06-19-2021, 09:05 PM by InsiteFX.)

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

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.
Reply
#6

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
Reply
#7

(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!!!

Learning CI4 from my works, from errors and how to fix bugs in the community

Love CI & Thanks CI Teams

Reply
#8

(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;
});
}
Reply




Theme © iAndrew 2016 - Forum software by © MyBB