Archive for July, 2009

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.