Adds several features to help make ZF2 applications cacheable.

Installs: 7 073

Dependents: 0

Stars: 7

Watchers: 4

Forks: 6

Language: PHP

1.1.3 2013-04-30 16:29 UTC


A ZF2 module that helps implement standards based caching built into the HTTP spec and gateway caches.

Build Status



  • PHP implementation of a gateway cache (COMING SOON)
  • Plugin for manipulating HTTP Cache headers (COMING SOON)
  • Plugin for dealing with expiration and validation (COMING SOON)
  • ESI View Helper
    • Very performant in Surrogate Capability mode - just renders ESI tag
    • Works w/o surrogate capability by running separate application lifecycle


It is recommended to add this module to your Zend Framework 2 application using Composer. After cloning ZendSkeletonApplication, add "socalnick/scn-http-cache" to list of requirements, then run php composer.phar install/update. Your composer.json should look something like this:

    "name": "zendframework/skeleton-application",
    "description": "Skeleton Application for ZF2",
    "license": "BSD-3-Clause",
    "keywords": [
    "homepage": "",
    "require": {
        "php": ">=5.3.3",
        "zendframework/zendframework": "2.*",
        "socalnick/scn-http-cache": "1.*"

Next add the required modules to config/application.config.php:

return array(
    'modules' => array(
    'module_listener_options' => array(
        'config_glob_paths'    => array(
        'module_paths' => array(



Varnish can be installed on any modern Linux distribution: It is also available via Homebrew on Mac OSX for development by running brew install varnish


This is the most basic Varnish configuration for a development environment. It sets the backend host / port, sets a request header indicating Surrogate Capability, and looks for the response Surrogate Control header to initiate ESI handling. Before running Varnish in a production environment, I highly encourage you to learn more about it at

backend default {
    .host = "";
    .port = "10088";

sub vcl_recv {
    # Set a header announcing Surrogate Capability to the origin
    # ScnEsiWidget sees this header and emits ESI tag for widgets
    set req.http.Surrogate-Capability = "varnish=ESI/1.0";

sub vcl_fetch {
    # Unset the Surrogate Control header and do ESI
    if (beresp.http.Surrogate-Control ~ "ESI/1.0") {
        unset beresp.http.Surrogate-Control;
        set beresp.do_esi = true;


Echo ESI in View Script

<div><?php echo $this->esi($this->url('route/to/recent/tweets')) ?></div>

Make the ESI Action

public function recentTweetsAction()
    $headers = $this->getResponse()->getHeaders();
    $cacheControl = new \Zend\Http\Header\CacheControl();
    $cacheControl->addDirective('s-maxage', '10');

    $viewModel = new ViewModel();

    return $viewModel;

Make a View Script for ESI Widget Action

    <li><?php echo date('h:i:s')?> @SocalNick: This is a recent tweet!</li>
    <li><?php echo date('h:i:s', time() - 10)?> @SocalNick: This is a slightly less recent tweet!</li>