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 . 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 (

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

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()
    $view = $this->getResource('view');
    $view->addFilterPath('My/View/Filter', 'My_View_Filter');

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.


  1. Horst


    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:
    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 *