PHP code example of silber / page-cache

1. Go to this page and download the library: Download silber/page-cache 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/ */

    

silber / page-cache example snippets


protected $middlewareGroups = [
    'web' => [
        \Silber\PageCache\Middleware\CacheResponse::class,
        /* ... keep the existing middleware here */
    ],
];

protected $middlewareAliases = [
    'page-cache' => \Silber\PageCache\Middleware\CacheResponse::class,
    /* ... keep the existing mappings here */
];

Route::middleware('page-cache')->get('posts/{slug}', 'PostController@show');

    

    namespace App\Http\Middleware;

    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\HttpFoundation\Response;
    use Silber\PageCache\Middleware\CacheResponse as BaseCacheResponse;

    class CacheResponse extends BaseCacheResponse
    {
        protected function shouldCache(Request $request, Response $response)
        {
            // In this example, we don't ever want to cache pages if the
            // URL contains a query string. So we first check for it,
            // then defer back up to the parent's default checks.
            if ($request->getQueryString()) {
                return false;
            }

            return parent::shouldCache($request, $response);
        }
    }
    

php artisan page-cache:clear

php artisan page-cache:clear {slug}

php artisan page-cache:clear {slug} --recursive

php artisan page-cache:clear categories --recursive

    php artisan make:middleware CacheResponse