11# Slim Framework HTTP Cache
22
33[ ![ Build Status] ( https://travis-ci.org/slimphp/Slim-HttpCache.svg?branch=master )] ( https://travis-ci.org/slimphp/Slim-HttpCache )
4+ [ ![ Latest Stable Version] ( https://poser.pugx.org/slim/http-cache/v )] ( //packagist.org/packages/slim/http-cache )
5+ [ ![ License] ( https://poser.pugx.org/slim/http-cache/license )] ( https://packagist.org/packages/slim/http-cache )
46
57This repository contains a Slim Framework HTTP cache middleware and service provider.
68
@@ -12,30 +14,38 @@ Via Composer
1214$ composer require slim/http-cache
1315```
1416
15- Requires Slim 3 .0.0 or newer.
17+ Requires Slim 4 .0.0 or newer.
1618
1719## Usage
1820
1921``` php
20- $app = new \Slim\App( );
22+ declare(strict_types=1 );
2123
22- // Register middleware
24+ use Psr\Http\Message\ResponseInterface as Response;
25+ use Psr\Http\Message\ServerRequestInterface as Request;
26+
27+ require __DIR__.'/../vendor/autoload.php';
28+
29+ $app = \Slim\Factory\AppFactory::create();
30+
31+ // Register the http cache middleware.
2332$app->add(new \Slim\HttpCache\Cache('public', 86400));
2433
25- // Fetch DI Container
26- $container = $app->getContainer ();
34+ // Create the cache provider.
35+ $cacheProvider = new \Slim\HttpCache\CacheProvider ();
2736
28- // Register service provider
29- $container['cache'] = function () {
30- return new \Slim\HttpCache\CacheProvider();
31- };
37+ // Register a route and pass the cache provider to the closure callback.
38+ $app->get(
39+ '/',
40+ function (Request $request, Response $response, array $args) use ($cacheProvider): Response {
41+ // Use the cache provider.
42+ $response = $cacheProvider->withEtag($response, 'abc');
3243
33- // Example route with ETag header
34- $app->get('/foo', function ($req, $res, $args) {
35- $resWithEtag = $this->cache->withEtag($res, 'abc');
44+ $response->getBody()->write('Hello world!');
3645
37- return $resWithEtag;
38- });
46+ return $response;
47+ }
48+ );
3949
4050$app->run();
4151```
0 commit comments