Minifying your Zend Framework views

When your website is getting alot of traffic, you’ll eventually reach the point where you want to tune things (or good habits). Next to backend and frontend caching, checking your expiry headers, optimizing images, … you can also minify your application’s output. Minifying or ‘minification’ is ‘the process of removing all unnecessary characters from source code, without changing its functionality‘.

Now keep in mind, it’s always better to load your CSS from an external stylesheet, this way it can get cached + you can use a very cool tool called http://code.google.com/p/minify/ . This leaves us with all the other code, your website’s HTML.

Using view output filters in Zend, it’s fairly easy to minify all your html/javascript/css output. In this blog post I’ll show you how to get things up and running. We’ll be using the same open source Minify library (http://code.google.com/p/minify/source/browse/#svn%2Ftrunk%2Fmin%2Flib)

Create a new view output helper (more about this):

<?php
require_once 'Zend/Filter/Interface.php';
require_once 'Minify/HTML.php';
require_once 'Minify/CSS.php';
require_once 'JSMin.php';
 
class My_View_Filter_Minify implements Zend_Filter_Interface
{
    public function filter($value) 
    {
    	return Minify_HTML::minify($value, array(
    	    'cssMinifier' => array('Minify_CSS', 'minify'),
            'jsMinifier' => array('JSMin', 'minify')
    	));
    }
}

Next and final step is letting our project know we want to use this new filter globally, so add the following code in your bootstrap:

public function _initMinifyHTML()
{
    $this->bootstrap('view');
    $view = $this->getResource('view');
    $view->addFilterPath('My/View/Filter', 'My_View_Filter');
    $view->setFilter('Minify');		
}

Freelance webdeveloper & Entreprenerd Specialized in developing, scaling and optimizing of succesful web applications, community websites and small/corporate business websites using technologies such as PHP 5, Laravel, HTML5, jQuery, .. Trying to find optimal Google love for every website. I'm always interested and open for new business opportunities.

5 Comments

  1. Horst

    Hy!

    This tutorial is very nice! But i have only one problem using the Filter in the view-files

    for example this:$this->minify($this->headLink()->appendStylesheet($this->baseUrl() . ‘/css/index.css’));

    gives me the following error message

    Fatal error: Class My_View_Filter_Minify contains 1 abstract
    method and must therefore be declared abstract or implement the
    remaining methods (Zend_Filter_Interface::filter) in D:Program Files (x86)ZendApache2htdocsapp_betaapplicationlayoutshelpersMinify.php on line 16

  2. Horst

    sorry for the double-posting, but it found out, that i dont have to use the filter in every view-file … it is enough to include the filter in the bootstrap like you described it.

    but then nothing happens, i see no difference in Yslow for example. when i try to use the minify-script manually for example:
    http://localhost/min/?f=css/layout.css
    it works!

    1. ChrisSchalenborgh

      Hi Horst,
      check the source code of your website in any browser, you’ll see it’s minified. Especially handy if you’re doing internal css / javascript.

Leave a Reply

Your email address will not be published. Required fields are marked *