Skip to content

Commit

Permalink
Update example code
Browse files Browse the repository at this point in the history
  • Loading branch information
adriansuter committed Jun 17, 2020
1 parent 1cb4a91 commit 49b7588
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Slim Framework HTTP Cache

[![Build Status](https://travis-ci.org/slimphp/Slim-HttpCache.svg?branch=master)](https://travis-ci.org/slimphp/Slim-HttpCache)
[![Latest Stable Version](https://poser.pugx.org/slim/http-cache/v)](//packagist.org/packages/slim/http-cache)
[![License](https://poser.pugx.org/slim/http-cache/license)](https://packagist.org/packages/slim/http-cache)

This repository contains a Slim Framework HTTP cache middleware and service provider.

Expand All @@ -12,30 +14,38 @@ Via Composer
$ composer require slim/http-cache
```

Requires Slim 3.0.0 or newer.
Requires Slim 4.0.0 or newer.

## Usage

```php
$app = new \Slim\App();
declare(strict_types=1);

// Register middleware
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;

require __DIR__.'/../vendor/autoload.php';

$app = \Slim\Factory\AppFactory::create();

// Register the http cache middleware.
$app->add(new \Slim\HttpCache\Cache('public', 86400));

// Fetch DI Container
$container = $app->getContainer();
// Create the cache provider.
$cacheProvider = new \Slim\HttpCache\CacheProvider();

// Register service provider
$container['cache'] = function () {
return new \Slim\HttpCache\CacheProvider();
};
// Register a route and pass the cache provider to the closure callback.
$app->get(
'/',
function (Request $request, Response $response, array $args) use ($cacheProvider): Response {
// Use the cache provider.
$response = $cacheProvider->withEtag($response, 'abc');

// Example route with ETag header
$app->get('/foo', function ($req, $res, $args) {
$resWithEtag = $this->cache->withEtag($res, 'abc');
$response->getBody()->write('Hello world!');

return $resWithEtag;
});
return $response;
}
);

$app->run();
```
Expand Down

0 comments on commit 49b7588

Please sign in to comment.