Created
November 12, 2015 14:14
-
-
Save ThijsFeryn/c5e2993bd2f02a6a8a0d to your computer and use it in GitHub Desktop.
Simple ESI rendering example using Silex and Varnish v4. The render_esi function in Twig renders ESI tags when the right Surrogate-Capability header is thrown, otherwise it uses internal subrequests.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": { | |
"silex/silex": "1.3.*", | |
"symfony/twig-bridge": "2.7", | |
"twig/twig": "1.23.*" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vcl 4.0; | |
backend default { | |
.host = "127.0.0.1"; | |
.port = "8080"; | |
} | |
sub vcl_recv { | |
set req.http.Surrogate-Capability = "key=ESI/1.0"; | |
} | |
sub vcl_backend_response { | |
if (beresp.http.Surrogate-Control ~ "ESI/1.0") { | |
unset beresp.http.Surrogate-Control; | |
set beresp.do_esi = true; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (php_sapi_name() == 'cli-server' && preg_match('/\.(?:png|jpg|jpeg|gif|css|js|ico|ttf|woff)$/', $_SERVER["REQUEST_URI"])) { | |
return false; | |
} | |
date_default_timezone_set('Europe/Brussels'); | |
require_once __DIR__.'/../vendor/autoload.php'; | |
use \Symfony\Component\HttpFoundation\Response; | |
use \Silex\Application; | |
$app = new Application(); | |
$app['debug'] = true; | |
$app->register(new \Silex\Provider\TwigServiceProvider(), array( | |
'twig.path' => dirname(__DIR__).'/views', | |
)); | |
$app->register(new Silex\Provider\HttpCacheServiceProvider()); | |
$app->register(new Silex\Provider\HttpFragmentServiceProvider()); | |
$app->register(new Silex\Provider\UrlGeneratorServiceProvider()); | |
$app->get('/', function () use ($app) { | |
$response = new Response($app['twig']->render('index.twig'),200); | |
$response->setSharedMaxAge(100); | |
return $response; | |
})->bind('home'); | |
$app->get('/header', function () use ($app) { | |
$response = new Response("Header: ".date('Y-m-d H:i:s'),200); | |
$response->headers->addCacheControlDirective('no-cache',true); | |
$response->headers->addCacheControlDirective('no-store',true); | |
return $response; | |
})->bind('header'); | |
$app->get('/footer', function () use ($app) { | |
$response = new Response("Footer: ".date('Y-m-d H:i:s'),200); | |
$response->setSharedMaxAge(10); | |
return $response; | |
})->bind('footer'); | |
$app->run(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>ESI</title> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
</head> | |
<body> | |
<h1>{{ render_esi(url('header')) }}</h1> | |
Main block: {{ "now"|date("Y-m-d H:i:s") }} | |
<h1>{{ render_esi(url('footer')) }}</h1> | |
</body> | |
</html> |
Author
ThijsFeryn
commented
Nov 12, 2015
- index.twig belongs in the "views" folder
- index.php belongs in the "web" folder
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment