Quantcast
Channel: CodeGround » CodeIgniter
Viewing all articles
Browse latest Browse all 5

How to Speed Up CodeIgniter

$
0
0

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:

$config['enable_hooks'] = TRUE;

Go to system/application/config/hooks.php and add the new hook:

$hook['display_override'][] = array(
    'class' => '',
    'function' => 'compress',
    'filename' => 'compress.php',
    'filepath' => 'hooks'
    );

Create the file system/application/hooks/compress.php and add the following code:

 $CI =& get_instance();
 $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',
  "//&lt;![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:

<?php /*Comment in my JavaScript*/ ?>

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.

$this->output->cache(60); // Will expire in 60 minutes

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.


Viewing all articles
Browse latest Browse all 5

Trending Articles