Using Minify in a Zend Framework project

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.

Advertisements
This entry was posted in PHP and tagged , , . Bookmark the permalink.

15 Responses to Using Minify in a Zend Framework project

  1. David says:

    Hey

    Just can across this post.However I cant seem to get it working on MVC.
    Please send me a working sample.

    Thanks

  2. David says:

    My email address is davykiash (a) gmail dot com. If you have a working sample please send it to this address.

    • mrgierer says:

      What problems are you facing? Where do you struggle?

      I don’t have a working example I can send you. If I find some time I will create one.

      • David says:

        At the point were am accessing the group url directly from my browser “http://localhost/minify/?g=js” I get the listing of the directory listing index.php,groupsConfig.php and config.php and not the js files.

        Am using WAMP for development but will soon deploy on LAMP.
        I have a feeling its some configuration am missing.

      • mrgierer says:

        Try adding index.php before ? or configure your server to use index.php automatically.

  3. David says:

    Hi

    I was way for a while but now back.

    I have changed my URL to read http://localhost/minify/index.php?g=js and what am now getting is a HTTP/1.0 400 Bad Request. A step closer to the solution I believe.

  4. David says:

    Hi

    Finally I got it working. I located a js script that was bringing that error.
    This tutorial rocks!!

  5. Pingback: Development Blog of PowerHour.at – Austrias first Happy Hour Search Engine » Minify Javascript and CSS

  6. Crow says:

    Very good job
    Thanks you for your help.
    Will help me for my projects

  7. Pingback: CSS, Javascript állományok tömörítése Zend Framework alatt | Running Code

  8. Bubba says:

    I appreciated your post, I ran across the same problem and ended up writing two drop-in helpers to manage it for me. You can see them at http://blog.hines57.com/2011/03/13/zendframework-minify/ – thanks again.

  9. Pingback: Minify + Zend framework | Kerek egy ég alatt

  10. Cormac says:

    Great article – I have just implemented with version 1.11.7 of the Zend Framework and it works a treat – thanks for the article.

  11. lk says:

    OR you can ignore : public/minify/index.js config.js and groupConfig.js files.
    And just put following code into any ControllerAction:

    set_include_path(LIBRARY_PATH . ‘/minify/lib’ . PATH_SEPARATOR . get_include_path());
    require ‘Minify.php’;
    echo Minify::combine(array(
    PUBLIC_PATH . ‘/app/view/MonthButton.js’
    ));

    … may be usefull to ext mvc users.

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