PHP code example of jkniest / htmlcache
1. Go to this page and download the library: Download jkniest/htmlcache library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
jkniest / htmlcache example snippets
'providers' => [
// ...
JKniest\HtmlCache\HtmlCacheServiceProvider::class,
// ...
]
protected $middlewareGroups = [
'web' => [
// ...
\JKniest\HtmlCache\Http\Middleware\CacheHtml::class,
// ...
],
// ...
]
protected $routeMiddleware = [
// ...
'htmlcache' => \JKniest\HtmlCache\Http\Middleware\CacheHtml::class
// ...
]
Route::middleware('htmlcache')->get('/route', 'Controller@action');
Route::group(['middleware' => 'htmlcache'], function () {
Route::get('/route', 'Controller@Action');
});
public function __construct()
{
$this->middleware('htmlcache');
}
/**
* HTML cache ignored routes
*/
'ignored' => [
'/admin',
'/another/ignored/route',
]
namespace App\Http\Middleware;
use Closure;
class HtmlCache extends \JKniest\HtmlCache\Http\Middleware\CacheHtml
{
}
protected function getCacheKey(string $page)
{
$prefix = config('htmlcache.prefix');
$locale = app()->getLocale();
$page = md5(trim($page, '/'));
if (config('htmlcache.user_specific')) {
$id = Auth::check() ? Auth::id() : -1;
return "{$prefix}{$page}_{$locale}_{$id}";
}
return "{$prefix}{$page}_{$locale}";
}
protected function getCacheKey(string $page)
{
$key = parent::getCacheKey($page);
$key .= date('D');
return $key;
}
protected $routeMiddleware = [
// ...
'htmlcache' => \App\Http\Middleware\HtmlCache::class,
// ...
];
shell
php artisan vendor:publish --tag=htmlcache
shell
php artisan cache:clear