The Slim Framework support forum has moved to This Tender forum is no longer maintained or monitored.

Slim 3: How to create cache pages?

lau.thiamkok's Avatar


25 Dec, 2015 04:05 AM

I know how to create a cache page with plain php, for instance,

    // @ref:
    // define the path and name of cached file
    $cachefile = 'cache/'.date('M-d-Y').'.php';
    // define how long we want to keep the file in seconds. I set mine to 1 hour.
    $cachetime = 3600;
    // Check if the cached file is still fresh. If it is, serve it up and exit.
    if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
        echo '<!-- cached page - '.date('l jS \of F Y h:i:s A', filemtime($cachefile)) . ' -->';
    // if there is either no file OR the file to too old, render the page and capture the HTML.
            output all your html here.
    // We're done! Save the cached content to a file
    $fp = fopen($cachefile, 'w');
    fwrite($fp, ob_get_contents());
    // finally send browser output

But how can I do it in Slim 3?

    use Slim\Http\Request;
    use Slim\Http\Response;
    require_once __DIR__ . '/../vendor/autoload.php';
    $app = new \Slim\App();
    // Routes:
    $app->get('/', function (Request $request, Response $response, array $args) {
        $response->getBody()->write('Hello, World!');
        return $response;

Any ideas?

  1. 1 Posted by Parker on 18 Feb, 2017 07:47 AM

    Parker's Avatar

    Great has been proven in among the above images. Total the installation process. the documents from various gadgets like phones, Fine.

  2. 2 Posted by roy on 06 Apr, 2017 06:29 AM

    roy's Avatar

    cost among others need to be taken into factor to consider. well matched by its spectacular exterior layout.

  3. 3 Posted by Johnson Charles on 06 May, 2017 01:21 PM

    Johnson Charles's Avatar

    I hope someone can help me out with you. By the way, can anyone update me about the Vikings Season 5 Cast

  4. 4 Posted by tony on 14 Jun, 2017 06:17 AM

    tony's Avatar

    Pokemon Go & Spotify with the Tutu Helper App. where you can locate Android apps and surf them the same way.

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:


Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac