Speed Up your Web Application by CodeIgniter

Now a days one of the big issue you have to focus that to High speed your web site performance. I have read a nice article” Performance tips for web applications” where focus the performance tips of web application. These tips can easily apply by codegIniter . Let see some of the tips below.

Some of the techniques are :

  • Enable GZIP Components.
  • Make Fewer Http Request .
  • Adds an Expires Header.
  • Minify JavaScript.

Enable GZIP Component :

In the system/application/config.ph

change the config variable true

   1: $config['compress_output'] = TRUE;

Make Fewer HTTP Request :

1. Caching the site.

Each time file loads from your web server, it generates an http request. So you can reduce number of http requests by caching some contents which are almost static or changes very rarely. if these contents are loaded from browser cache, there will be no request for them over HTTP.

So you can easily caching your web Pages in the CodeIgniter :

Enabling Caching

To enable caching, put the following tag in any of your controller functions:

   1: $this->output->cache(n);

Where n is the number of minutes you wish the page to remain cached between refreshes.

The above tag can go anywhere within a function. It is not affected by the order that it appears, so place it wherever it seems most logical to you. Once the tag is in place, your pages will begin being cached.

To set data you can do it by the

   1: $this->output->set_output($data);

Also you can manually set header :

$this->output->set_header();

Permits you to manually set server headers, which the output class will send for you when outputting the final rendered display. Example:

   1: $this->output->set_header("HTTP/1.0 200 OK");
   2: $this->output->set_header("HTTP/1.1 200 OK");
   3: $this->output->set_header('Last-Modified: '.gmdate('D, d M Y H:i:s', $last_update).' GMT');
   4: $this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate");
   5: $this->output->set_header("Cache-Control: post-check=0, pre-check=0", false);
   6: $this->output->set_header("Pragma: no-cache");

2. join multiple css or js file into single css or js file .

It can be done in many ways . But in the CodeIgniter forum i find some ways to do that . You can check out here. The below code is the forum message :

Following is the controller to join multiple cs or jss
file in to one, and hence making fewer server request to speed up the things little bit.

Just add following in to your controllers. save as file.php Set the path of your
js and css files accordingly.

   1: <?php
   2: class File extends Controller
   3: {
   4: function File()
   5:     {
   6:         parent::Controller();
   7: /*Set your own path here*/
   8: $this->_path = "./public/";
   9: }
  10: function index()
  11: {
  12: echo "";
  13: }
  14: function js()
  15: {
  16:         $segs = $this->uri->segment_array();
  17:         foreach ($segs as $segment)
  18: {
  19:             $filepath = $this->_path.$segment.'.js';
  20:             if(file_exists($filepath))
  21: {
  22:                 readfile($filepath);
  23: }
  24:         }
  25:     }
  26: function css()
  27: {
  28:         $segs = $this->uri->segment_array();
  29:         foreach ($segs as $segment)
  30: {
  31:             $filepath = $this->_path.$segment.'.css';
  32:             if(file_exists($filepath))
  33: {
  34:                 readfile($filepath);
  35: }
  36:         }
  37:     }
  38: }
  39: ?>

Now you can add any number of files with just single line of code,and with single
request.
For e.g. If you want to include two css file , foo.css and bar.css , the code is ,

   1: <link rel='stylesheet' type='text/css' media='all' href='<?php echo site_url("file/css/foo/bar");?>' />

The alternative of that one is :

got a script from somewhere that gzips the css/js, caches the compilation of the files etc. Since I generally use jQuery I my controller also has the function jQuery. I can now do

<?PHP echo site_url(asset/jquery); ?>

This redirects to a similar function as you have but a bit shorter.

Adds an Expires Header:

you can add expire header by the Ci library output class.

Minify JavaScript:

You can minify javascript by packed, Minify.

  1. Hi dude ,

    Great post . Can u be good enough to let me know , where i could find training material for Codeigniter ?

    • Leonardo Hessel
    • July 1st, 2010

    Great post !!!

  2. great post!!! it really helped me with my gzip problem. ty

    • Khalil
    • August 18th, 2010

    Very good post, Thank you

  3. Thanks for share. g8 post.
    I use gzip in http://www.coolajax.net and it’s worked nice.

  4. thanks for this post.
    on set_header(), where the value of $last_update was initialized?

  5. Very clear site, thankyou for this post.

  6. Nice article material Thank’s alot

  7. Clearing Cache on DB Update/Inserts/Deletes?

    I have a page that does double duty: it displays a big table of records, and also has an `add new record` form.

    I want to use CI-Cache on this page, but I also need for the cache to be deleted when a new record is added to the database.

    The problem is, that CodeIgniter doesn’t take into account whether stuff has been submitted via $_POST when deciding to display a cached page.

  8. Hey very nice blog!! Man Excellent Amazing I will bookmark your site and take the feeds also…I am happy to find numerous useful info here in the post, we need develop more strategies in this regard, thanks for sharing…..

  9. Great Post, I’m fan of Codeigniter, just great to the beginners in the Page Optimizing

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: