PHPackages                             spektra2147/laravel-httpcache - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Caching](/categories/caching)
4. /
5. spektra2147/laravel-httpcache

ActiveLibrary[Caching](/categories/caching)

spektra2147/laravel-httpcache
=============================

HttpCache for Laravel

1.0(2y ago)0424MITPHPPHP ^8

Since Nov 11Pushed 2y agoCompare

[ Source](https://github.com/spektra2147/laravel-httpcache)[ Packagist](https://packagist.org/packages/spektra2147/laravel-httpcache)[ RSS](/packages/spektra2147-laravel-httpcache/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (5)Versions (2)Used By (0)

HttpCache for Laravel
---------------------

[](#httpcache-for-laravel)

[![Tests](https://github.com/barryvdh/laravel-httpcache/workflows/Tests/badge.svg)](https://github.com/barryvdh/laravel-httpcache/actions)[![Packagist License](https://camo.githubusercontent.com/8d47e99d1e3ff0e5f340d401c29cf46d680b717ac031f62f1f86b7a313674479/68747470733a2f2f706f7365722e707567782e6f72672f62617272797664682f6c61726176656c2d6874747063616368652f6c6963656e73652e706e67)](http://choosealicense.com/licenses/mit/)[![Latest Stable Version](https://camo.githubusercontent.com/2dc54a63204decd2415c11c49fb42b620a3f07c40e2efaffd9696bd70379874c/68747470733a2f2f706f7365722e707567782e6f72672f62617272797664682f6c61726176656c2d6874747063616368652f76657273696f6e2e706e67)](https://packagist.org/packages/barryvdh/laravel-httpcache)[![Total Downloads](https://camo.githubusercontent.com/3227e5015dbd4698c62fac7351ec24dcdebdd77bafe5fd62c67c12690f804bae/68747470733a2f2f706f7365722e707567782e6f72672f62617272797664682f6c61726176656c2d6874747063616368652f642f746f74616c2e706e67)](https://packagist.org/packages/barryvdh/laravel-httpcache)[![Fruitcake](https://camo.githubusercontent.com/28d7584b52e33d7b4ffb6b1bef8b89b6e598adb45c8c0d5f80349c1c304f385e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f506f776572656425323042792d467275697463616b652d6232626333352e737667)](https://fruitcake.nl/)

Laravel can use [HttpKernelInterface Middlewares](http://stackphp.com/middlewares/), so also [HttpCache](http://symfony.com/doc/current/book/http_cache.html). This package provides a simple ServiceProvider to get you started with HttpCache.

First, require this package with composer

```
composer require barryvdh/laravel-httpcache

```

After updating, add the ServiceProvider to the array of providers in app/config/app.php

```
'Barryvdh\HttpCache\ServiceProvider',

```

You can now add the Middleware to your Kernel:

```
'Barryvdh\HttpCache\Middleware\CacheRequests',

```

Caching is now enabled, for public responses. Just set the Ttl or MaxSharedAge

```
Route::get('my-page', function(){
   return Response::make('Hello!')->setTtl(60); // Cache 1 minute
});
```

You can use the provided `SetTtl` middleware in your Kernel to simplify this:

```
protected $routeMiddleware = [
    // ...
    'ttl' => \Barryvdh\HttpCache\Middleware\SetTtl::class,
];

Route::get('my-page', function(){
   return 'Hello'
})->middleware('ttl:60'); // Cache 1 minute
```

Publish the config to change some options (cache dir, default ttl, etc) or enable ESI.

```
$ php artisan vendor:publish --provider="Barryvdh\HttpCache\ServiceProvider"

```

### Direct approach, without ServiceProvider

[](#direct-approach-without-serviceprovider)

> Note: This is still in beta, test with caution. It should be faster, but less flexible because it starts earlier.

You can also wrap the Kernel in the HttpCache, in your public/index.php. Replace the 'Run The Application' part like this:

```
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

$kernel = \Barryvdh\HttpCache\CacheKernel::wrap($kernel);

$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);

$response->send();

$kernel->terminate($request, $response);

```

### ESI

[](#esi)

Enable ESI in your config file and add the Esi Middleware to your Kernel:

```
'Barryvdh\HttpCache\Middleware\ParseEsi',

```

You can now define ESI includes in your layouts.

```
