CakeHtmlHelper speed up your site enabling html cache - Cakephp 2.x

I have rewrite the Matt Curry Html Cache Plugin. Taken from – Cake’s core cache helper is great, but the files it outputs are PHP files, so it will never be as fast as straight HTML files. This HTML Cache Helper writes out pure HTML, meaning the web server doesnt have to touch PHP when a request is made. Okey, first you must have mod_rewrite enable to make the html cache working;

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_METHOD} ^GET$
    RewriteCond %{DOCUMENT_ROOT}/cache/$1/index.html -f
    RewriteRule ^(.*)$ /cache/$1/index.html [L]

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

Now create the helper in the file View/Helper/CacheHtmlHelper.php

// Original idea from Matt Curry, rewritten by Eugenio Fage
App::uses('AppHelper', 'View/Helper');

App::uses('Folder', 'Utility');
App::uses('File', 'Utility');

class CacheHtmlHelper extends AppHelper {
    private $CacheHtmlStrategy=null;

    public function __construct(View $view, $settings = array()) {
        parent::__construct($view, $settings);

        if(!class_exists($settings['CacheHtmlStrategy'])) $settings['CacheHtmlStrategy']='CacheHtmlStrategy';
        $this->CacheStrategy=new $settings['CacheHtmlStrategy']();

    public function afterLayout($viewFile) {

class CacheHtmlStrategy{
    public function save($request,$content){
            $File = new File($this->url2file($request->url), true);

    public function url2file($url){
            $path.=implode(DS, array_filter(explode('/', $url))).DS;
            return $path.'index.html';

    public function rendercache($url=null){
                    if(isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO']){

                    $fp = fopen($file, 'r');

Now, each time you want to cache an action add the helper in the controller; function someaction(){ $this->helpers[]=’HtmlCache’; }

You could create your own cache strategy to save the files in diferents path or doing some crazy thing, for example: Config/Crazyhtmlcacheconfig.php

class CrazyHtmlStrategy extends CacheHtmlStrategy{
    public function url2file($request){
            $path.=implode(DS, array_filter(explode('/', $request->url))).DS;
            return $path.'index.html';

Then add in your bootstrap; config(‘Crazyhtmlcacheconfig’);

And when you add the helper

$this->helpers[‘HtmlCache’]=array(‘CacheHtmlStrategy’=>’CrazyHtmlStrat egy’);

You could also try to render the files using php instead of the .htaccess routing, just have to add this in the bootstrap;

// use CacheHtmlStrategy or the strategy that you are currently using
$cache=new CacheHtmlStrategy();

Cheers to all! Written by Eugenio Fage