The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

Slim 3: How to create cache pages?

lau.thiamkok's Avatar

lau.thiamkok

25 Dec, 2015 04:05 AM

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

```
    // @ref: http://wesbos.com/simple-php-page-caching-technique/
    //
    // 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)) {
        include($cachefile);
        echo '<!-- cached page - '.date('l jS \of F Y h:i:s A', filemtime($cachefile)) . ' -->';
        exit;
    }
    
    // if there is either no file OR the file to too old, render the page and capture the HTML.
    ob_start();
    ?>
        <html>
            output all your html here.
        </html>
    <?php
    
    // We're done! Save the cached content to a file
    $fp = fopen($cachefile, 'w');
    fwrite($fp, ob_get_contents());
    fclose($fp);
    
    // finally send browser output
    ob_end_flush();
```

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;
    });
    
    $app->run();
```

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. https://shareitforpcdl.com/download-shareit-for-pc-windows-mac/ 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. http://www.kitchenopedia443.sitew.in/#Vacuum_Sealer_Reviews.A 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. https://itutuhelper.com/tutu-helper-apk-download/ 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

Generic

? 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