PHPackages                             jkniest/htmlcache - 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. jkniest/htmlcache

AbandonedArchivedLibrary

jkniest/htmlcache
=================

A simple HTML Cache middleware for laravel

1.0.5(7y ago)364.0k↓100%1[1 issues](https://github.com/jkniest/HTMLCache/issues)MITPHP

Since Aug 28Pushed 5y ago1 watchersCompare

[ Source](https://github.com/jkniest/HTMLCache)[ Packagist](https://packagist.org/packages/jkniest/htmlcache)[ RSS](/packages/jkniest-htmlcache/feed)WikiDiscussions develop Synced today

READMEChangelog (7)Dependencies (3)Versions (8)Used By (0)

Laravel HTML Cache
==================

[](#laravel-html-cache)

[ ![Build](https://camo.githubusercontent.com/d3893dbe9f518eb438e113f1f2cf622a89d0843c96d8d5ea3423694f452df785/68747470733a2f2f7472617669732d63692e636f6d2f6a6b6e696573742f48544d4c43616368652e7376673f746f6b656e3d5632484646434c63364e566e7873716a71443976266272616e63683d646576656c6f70) ](https://travis-ci.com/jkniest/HTMLCache) [![Latest Stable Version](https://camo.githubusercontent.com/8d0a142285d4942210afbb45d981a3c6920fefb1c38699870d7d91892c9a06ef/68747470733a2f2f706f7365722e707567782e6f72672f6a6b6e696573742f68746d6c63616368652f762f737461626c65)](https://packagist.org/packages/jkniest/htmlcache) [![Total Downloads](https://camo.githubusercontent.com/6a5dc9a8ba34de15ef013d3d91aa412796caa3dd58abdb268c3b3f247c793b3f/68747470733a2f2f706f7365722e707567782e6f72672f6a6b6e696573742f68746d6c63616368652f646f776e6c6f616473)](https://packagist.org/packages/jkniest/htmlcache) [![License](https://camo.githubusercontent.com/6b6c74af0f9a9425e98026f3b11064f6fa2fd74a69adc9c7625fc48a0af22a51/68747470733a2f2f706f7365722e707567782e6f72672f6a6b6e696573742f68746d6c63616368652f6c6963656e7365)](https://packagist.org/packages/jkniest/htmlcache) [ ![StyleCI](https://camo.githubusercontent.com/fe3d7c02943d5a68485d8b3010cb0a4007c7125dc2684db8137ad1c4de1946cd/68747470733a2f2f7374796c6563692e696f2f7265706f732f3130303336393136302f736869656c643f6272616e63683d646576656c6f70267374796c653d666c6174) ](https://styleci.io/repos/100369160)

---

This package **speeds up** your laravel application by caching the final rendered **html** and **header fields**. So your database queries and view loading algorithms don't need to run every single page load.

This package is made for you if you have a lot of static pages (or pages that don't change very often). That means, **portfolios**, **blogs**, **landing pages** and more.

And it is highly customizable: You can even cache the same page for every user different, allowing that you can cache for example their account page or dashboard without worrying that another user can see these cached pages.

**One benefit against much other html caches:** It will also cache the pages based on language, and (optionally) user id. And if there are special cases (for example a specific session value) that needs to be used to generate multiple versions of the same page, the middleware can be easily modified.

---

Table of contents
-----------------

[](#table-of-contents)

1. [Installation](#installation)
2. [Using](#using)
    2.1. [For all web routes](#for-all-web-routes)
    2.2. [Only for specific routes](#only-for-specific-routes)
3. [When will pages not be cached?](#when-will-pages-not-be-cached)
4. [Configuration](#configuration)
    4.1. [Enable / Disable cache](#enable--disable-cache)
    4.2. [Caching prefix](#caching-prefix)
    4.3. [Caching time](#caching-time)
    4.4. [User specific caching](#user-specific)
5. [Ignoring routes](#ignoring-routes)
6. [Clear cache](#clear-cache)
7. [Override middlware](#override-middlware)
8. [Roadmap](#roadmap)
9. [License](#license)

---

Installation
------------

[](#installation)

The installation process is very straight-forward. It's like any other laravel package.

1. Add the package as a composer dependency by running the following command inside the console:

```
composer require jkniest/htmlcache
```

**If you are using laravel 5.5 or higher the installation is already finished. You now have multiple ways to use the middleware (see [Using](#using)).**

Otherwise, if you use laravel 5.4 or lower go to step 2:

2. Add the package service provider in your packages configuration. Open up the `config/app.php` file and the following into your `providers` array:

```
'providers' => [
	// ...
	JKniest\HtmlCache\HtmlCacheServiceProvider::class,
	// ...
]
```

3. Done! Now you can use this middleware inside your project. You now have multiple ways to use the middleware (see [Using](#using))

---

Using
-----

[](#using)

### For all web routes

[](#for-all-web-routes)

If you want to apply this middleware to every single web route (web route = no api route) you can add this to your global web middleware group.

Add the following line into your `app/Http/Kernel.php` file inside the middleware-group web variable:

```
protected $middlewareGroups = [
    'web' => [
        // ...
        \JKniest\HtmlCache\Http\Middleware\CacheHtml::class,
        // ...
    ],
    // ...
]
```

Now every single web-route uses the html cache. And it will just work! You don't have to do another thing. If you want to dig deeper (and maybe want to ignore some specific routes), see [Configuration](#configuration).

### Only for specific routes

[](#only-for-specific-routes)

If you don't want to apply this middleware on any web-route you can use it only in specific routes or route-groups.

First you should add an alias to your `app/Http/Kernel.php` file:

```
protected $routeMiddleware = [
    // ...
    'htmlcache' => \JKniest\HtmlCache\Http\Middleware\CacheHtml::class
    // ...
]
```

Now you can define the middleware in routes or route groups:

**Single Routes:** (in `routes/web.php`)

```
Route::middleware('htmlcache')->get('/route', 'Controller@action');
```

**Route groups:** (in `routes/web.php`)

```
Route::group(['middleware' => 'htmlcache'], function () {
    Route::get('/route', 'Controller@Action');
});
```

**Controllers:** (in `app/Http/Controllers/...`)

```
public function __construct()
{
    $this->middleware('htmlcache');
}
```

---

When will pages not be cached?
------------------------------

[](#when-will-pages-not-be-cached)

In a few cases the pages will not be cached:

1. If the HTTP method is not `GET`
2. If the HTMLCache is disabled in the configuration
3. If the path of the current page is ignored in the configuration
4. If there are validation errors
5. If the status code is not 200

---

Configuration
-------------

[](#configuration)

You can configure nearly anything inside the `.env` file.

### Enable / Disable cache

[](#enable--disable-cache)

Enable or **disable** the whole caching system. If this value is set to false no new pages will be cached and no old caches will be loaded.

```
HTML_CACHE_ENABLED=true

```

### Caching prefix

[](#caching-prefix)

A simple prefix that will be added to all cache keys. The default key is `html_`. So a cache key would look something like this: `html_aboutus_en`

```
HTML_CACHE_PREFIX="html_"

```

### Caching time

[](#caching-time)

The amount of minutes every single page should be cached. After these minutes the cache will be regenerated on the next page load. The default value is `360 Minutes` (= 6 hours)

```
HTML_CACHE_MINUTES=360

```

### User-Specific

[](#user-specific)

If this value is set to true the cache key will contain the user id. If the user is not signed in, a `-1` will be used instead (so each guest does share the cached result and every signed in user does have it's own cached version). This is useful if you want to cache user-specific pages, like a dashboard. The default value is `false`.

```
HTML_CACHE_USER_SPECIFIC=false

```

---

Ignoring routes
---------------

[](#ignoring-routes)

It is possible to ignore specific routes. You need to publish the default configuration by using this command:

```
php artisan vendor:publish --tag=htmlcache
```

This will create a new file in your project: `config/htmlcache.php`. There you can configure any ignored routes by simply adding these to the `ignored` array:

```
    /**
     * HTML cache ignored routes
     */
    'ignored' => [
        '/admin',
        '/another/ignored/route',
    ]
```

---

Clear cache
-----------

[](#clear-cache)

The html cache package uses the default laravel cache helpers. So you can run the artisan command to clear the cache:

```
php artisan cache:clear
```

This will remove every cached version of this plugin (and also everything else). It is recommended to put this in your deployment workflow (for example in the deployment script in forge or as a deployment hook in envoyer)

---

Override middlware
------------------

[](#override-middlware)

It is possible to override the middleware. So you could override the cache-key generation. In this short tutorial we will add another field to the cache key generation (the current weekday).

Let's say you have a dashboard and all data on this dashboard will only update every weekday. In the default implementation you would have to set a maximum cache time (for example 1 day) but you can't be sure that the cache was generated exactly on 0am.

The simplest solution would be to override the middleware and extends the cache-key generation.

1. Create a new middleware inside the `app/Http/Middleware` folder. You can name it like you want.. in this case I will name it: `HtmlCache.php`

```
