Using rakaz Combine With Your CakePHP Application

Combine is a small PHP script and some clever URL rewriting designed to speed up the loading of pages that use many or large css and javascript files.

It is good but how can you integrate it with your CakePHP project? And solution comes with the question :).

Download a copy of combine.php and drop it into your “app/webroot” directory (or whatever your application folder name is XXX/webroot/).

Open the file in your text editor and change lines:

$cachedir = dirname(__FILE__) . '/cache';
$cssdir   = dirname(__FILE__) . '/css';
$jsdir    = dirname(__FILE__) . '/javascript';

with

$cachedir = dirname(__FILE__) . '/../tmp/cache';
$cssdir   = dirname(__FILE__) . '/css';
$jsdir    = dirname(__FILE__) . '/js';

Open your “app/webroot/.htaccess” file and add those two lines

RewriteRule ^css/(.*\.css) combine.php?type=css&files=$1
RewriteRule ^js/(.*\.js) combine.php?type=javascript&files=$1

That’s all! Check download times of your css and javascript files with a tool like Firebug. A fatty 527KB ext-all.js javascript framework file compressed to 140KB! Amazing ha?

rakaz combine

  • http://www.facebook.com/profile.php?id=572342110 Simon Potter

    Brilliant thanks Erhan.

  • http://erhanabay.com Erhan Abay

    It should be fine.

    Here is what I am using:


    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

    RewriteRule ^css/(.*\.css) combine.php?type=css&files=$1
    RewriteRule ^js/(.*\.js) combine.php?type=javascript&files=$1

  • http://www.falise.com Sander

    Nice! But it doesn’t seem to work … yet!
    Any specific order needed in the .htaccess file?
    Mine looks like this right now:

    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f

    RewriteRule ^css/(.*\.css) combine.php?type=css&files=$1
    RewriteRule ^js/(.*\.js) combine.php?type=javascript&files=$1

    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]