CodeIgniter and Minify Redux

My previous article about CodeIgniter and Minify was a relative hit (relative only to my other posts that is), but some thinking since the original version mixed with some of the great comments on that post have prompted me to update the integration points a little to improve the flexibility and maintainability. Requiring a new method inside the MY_Includes.php file for each controller got to be a bit of a bear. Also, the wacky mapping that I did in the init method was overkill. Lastly, the code from the original post broke when CodeIgniter 1.7.1 was release because of the new support for name aliasing of library classes, so it needed an update anyway.

What I decided to do was to export the mapping to a config file. See an example config file here. This file goes into the application/config directory. You probably also want to update application/config/autoload.php to autoload that config file:

$autoload[‘config’] = array(‘minify_config’);

It is assumed that all JavaScript files are in the same location on disk and all CSS files are all in the same location on disk. It also assumes that all JavaScript files end with js and all CSS files end with css. The rest (mostly paths) can all be configured. I don’t show it but I have the absolute paths to the JavaScript/CSS files in the standard CodeIgniter config file.

The minify_config.php config file sets up a hash where the key is the controller name and the value is an array of JavaScript or CSS filenames. For simplicity’s sake the file name array is only one-dimensional, so you’ll notice the use of array_merge in some spots.

The MY_Includes class itself is greatly simplified now, but at its core performs the same task as the previous version, building a list of files to be included and passing them off to Minify for processing. The includetag.php controller is unchanged.

So the new and improved steps for integration are:

  1. Download MY_Includes.php (here) and put it in your /system/applications/libraries directory
  2. Download minify_config.php (here) and put it in your application/config directory
  3. Edit the init method inside of MY_Includes.php to include the correct path to your Minify installation
  4. (Optional) Edit the compileTags method inside of MY_Includes.php to include any special rules. Out of the box it will include a config item named global on every request, then check to see if there are any controller-specific files to include.
  5. Download includetag.php (here) and put it in the /system/applications/controllers directory
  6. Add the two code fragments commented with the text “for globally included header file” below to the appropriate file in your application
  7. Fire it up
//for globally included header file (e.g. header.php)
//so know which CSS or JS files to include
$pageName = $this->uri->segment(1, 0);
$pageName .= “/” . $this->uri->segment(2, “index”);
$this->CI->load->library(“MY_Includes”, $pageName);
<!– for globally included header file (e.g.header.php) –>
<link rel=”stylesheet” href=”<?= $this->CI->my_includes->cssTag(); ?>” type=”text/css” media=”screen” />
<script src=”<?= $this->CI->my_includes->jsTag(); ?>” type=”text/javascript” charset=”utf-8″></script>

Feel free to post a comment if you have troubles and I’ll walk you through it or edit the post to fix any errors as needed.

Be Sociable, Share!
6 comments

6 Comments so far

  1. Ki on July 29th, 2009

    Great Work!
    Have been using your class for some time now and had a great improvement on loading speed and ease of js and css management. Thanks!

  2. Phil Nelson on November 20th, 2009

    Just what I needed. Thanks.

  3. bolli.us on February 3rd, 2010

    hi there!

    thanks for your efforts in this matter.
    unfortunately I’m having a problem with the step 7 of this article. it states
    1. Undefined property: CI_Loader::$my_includes
    2. Call to a member function cssTags() on a non-object
    I don’t get it why the my_includes doesn’t become an object. hope you can help me.

    thanks! michael

  4. Craig Myles on September 22nd, 2010

    Hi, appreciate the work on this. However, it would be really great if you could provide more information on some of the steps above, like full code examples. There is also some mistakes in your code like “MY_Includes’ should be “MY_Includes” so people copying and pasting may have issues. Cheers

  5. Matt on November 19th, 2010

    For the person having issues with cssTags(). You need to have this above your load:
    $this->CI =& get_instance();

  6. Ryan Charmley on March 4th, 2011

    For a lightweight easy-to-implement solution I recommend checking out http://codeigniter.com/wiki/Carabiner/

Leave a Reply