a Sensio Labs Product

The PHP micro-framework
based on the Symfony2 Components

Questions & Feedback

License

Creative Commons License Silex documentation is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.

HttpCacheServiceProvider

The HttpCacheProvider provides support for the Symfony2 Reverse Proxy.

Parameters

  • http_cache.cache_dir: The cache directory to store the HTTP cache data.
  • http_cache.options (optional): An array of options for the HttpCache constructor.

Services

Registering

$app->register(new Silex\Provider\HttpCacheServiceProvider(), array(
    'http_cache.cache_dir' => __DIR__.'/cache/',
));

Usage

Silex already supports any reverse proxy like Varnish out of the box by setting Response HTTP cache headers:

use Symfony\Component\HttpFoundation\Response;

$app->get('/', function() {
    return new Response('Foo', 200, array(
        'Cache-Control' => 's-maxage=5',
    ));
});

Tip

If you want Silex to trust the X-Forwarded-For* headers from your reverse proxy, you will need to run your application like this:

use Symfony\Component\HttpFoundation\Request;

Request::trustProxyData();
$app->run();

This provider allows you to use the Symfony2 reverse proxy natively with Silex applications by using the http_cache service:

$app['http_cache']->run();

The provider also provides ESI support:

$app->get('/', function() {
    return new Response(<<<EOF
<html>
    <body>
        Hello
        <esi:include src="/included" />
    </body>
</html>

EOF
    , 200, array(
        'Cache-Control' => 's-maxage=20',
        'Surrogate-Control' => 'content="ESI/1.0"',
    ));
});

$app->get('/included', function() {
    return new Response('Foo', 200, array(
        'Cache-Control' => 's-maxage=5',
    ));
});

$app['http_cache']->run();

For more information, consult the Symfony2 HTTP Cache documentation.

This website is powered by Silex and Twig. The Silex logo is © 2010,2011 Sensio Labs