3 Simple steps to speed up your CodeIgniter applications. HTML, Page Caching and Gzip.
1. Compress HTML output
We will need to create a hook. This hook will remove any whitespace in the HTML output.
Go to system/application/config/config.php and enable hooks:
Go to system/application/config/hooks.php and add the new hook:
'class' => '',
'function' => 'compress',
'filename' => 'compress.php',
'filepath' => 'hooks'
);
Create the file system/application/hooks/compress.php and add the following code:
$buffer = $CI->output->get_output();
$search = array(
'/\>[^\S ]+/s',
'/[^\S ]+\</s',
'/(\s)+/s', // shorten multiple whitespace sequences
'#(?://)?<!\[CDATA\[(.*?)(?://)?\]\]>#s' //leave CDATA alone
);
$replace = array(
'>',
'<',
'\\1',
"//<![CDATA[\n".'\1'."\n//]]>"
);
$buffer = preg_replace($search, $replace, $buffer);
$CI->output->set_output($buffer);
$CI->output->_display();
IMPORTANT: if you have any JavaScript within the HTML (not in a .JS file) make sure you don’t have JavaScripts comments in it.
Use the PHP comment system instead:
2. Cache functions
You can cache certain functions in your controllers.
Add the following line and it will create a cached HTML of the page.
Make sure the Application/cache folder is writable.
More info http://codeigniter.com/user_guide/general/caching.html
3. Enable Gzip Compression
Go to application/config/config.php and enable the Gzip compression:
|--------------------------------------------------------------------------
| Output Compression
|--------------------------------------------------------------------------
|
| Enables Gzip output compression for faster page loads. When enabled,
| the output class will test whether your server supports Gzip.
| Even if it does, however, not all browsers support compression
| so enable only if you are reasonably sure your visitors can handle it.
|
| VERY IMPORTANT: If you are getting a blank page when compression is enabled it
| means you are prematurely outputting something to your browser. It could
| even be a line of whitespace at the end of one of your scripts. For
| compression to work, nothing can be sent before the output buffer is called
| by the output class. Do not "echo" any values with compression enabled.
|
*/
$config['compress_output'] = TRUE;
The post How to Speed Up CodeIgniter appeared first on CodeGround.