Minify is a project to help you improve your website performance by grouping, compressing and caching CSS and/or JS files.
To use it in a Zend Framework project I suggest the following steps:
1. Download und unpack Minify
2. Split up Minify according to the Zend Framework project structure
Assuming you are using the standard directory structure suggested for Zend Framework projects, copy the contents of “min/lib” from the Minify project to “library/minify/lib” in your project. The files “config.php”, “groupsConfig.php” and “index.php” go to a new directory under “public” called “minify”.
This steps ensures that only the necessary files are located in the public area of the project.
3. Configure Minify
Next we need to configure Minify. I suggest using the group feature to summarize all Javascript files into single URL – same goes for CSS.
Here is an example configuration:
return array(
'js' => array('//javascripts/prototype.js', '//javascripts/application.js'),
'css' => array('//stylesheets/application.css')
);
Of course you can have as many files in the groups as you like. If you need more – or different combination of files in groups throughout your website create groups accordingly.
You can test your groups by accessing the group url directly. E.g. the javascript url from the above example can be accessed through: http://server.name/minify/?g=js
This should display the grouped and minified javascript files.
4. Using the minified files
Now you can just use the above url to include the configured groups in your application, by using:
<script type="text/javascript" src="minify/?g=js"></script>
While this would work perfectly can could go even further. The above example will tell the browser to cache the contents for a given time (see config.php). This introduces a little conflict: If the timeout is small the cache will be refreshed more often than necessary. A higher value makes it difficult if you need to change something in your files, since not all browsers will get the changes when they appear.
Minifys solution to this is to add a second parameter which identifies any changes to the group. As long as this (numeric) identifier stays the same, there is no need to delivery the file again – caching is preferred. Once the identifier changes the files will be served to the browser again.
Sounds complicated ? It’s not ! Minify brings some handy utilities making it easy.
Since we are using Zend Framework I would suggest to create a helper to encapsulate all this.
5. The minify helper
To let a helper do all the work with the caching identifier we change the SCRIPT-Tag to:
<script type="text/javascript" src="<?php echo $this->Minify('js') ?>"></script>
So it our soon-to-be-create Helpers job to construct the url to Minify group and take care about that caching stuff.
Here is the helper code:
class My_View_Helper_Minify {
public function setView(Zend_View_Interface $view) {
$this->view = $view;
}
public function Minify($group) {
require_once 'minify/lib/Minify/Build.php';
static $builds = array();
static $gc = false;
if (false === $gc) {
$gc = (require '../public/minify/groupsConfig.php');
}
if (! isset($builds[$group])) {
$builds[$group] = new Minify_Build($gc[$group]);
}
return $builds[$group]->uri($this->view->baseUrl.'/minify/?g='.$group);
}
}
Note: Most if this code is taken from Minifys utils.php and packed into a ZF helper.
Summary
I implemented theses steps in 3 projects so far and the results are very good. I hope this helps to serve Javascript and CSS files more elegantly.