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

Help with HTTP Caching (LastModified)

Josh Crawford's Avatar

Josh Crawford

30 Apr, 2012 02:20 AM

I've just been playing around with the HTTP Caching capabilities of Slim, using LastModified specifically, and just had a question about the best way to trigger updating the timestamp supplied to the LastModified function?

The example states to use $app->lastModified(1286139652); which is fair enough, but how do we update this timestamp? Our database will be updated at various intervals, which, when updated, need to invalidate (update) the above timestamp.

My first though what to create another GET route, which updates an app variable with the current timestamp - eg $app->config('last_modified', time()); However, this variable doesn't seem to be persistent, and doesn't 'save' for the next request.

Any help would be appreciated!


  1. Support Staff 1 Posted by Josh Lockhart on 30 Apr, 2012 02:34 AM

    Josh Lockhart's Avatar

    You should only update the given resource's Last-Modified value when the resource itself is actually updated. Otherwise, you defeat the purpose of using HTTP caching. You'll also want to set an Expires header using $app->expires(), so that the browser will actually cache the resource rather than send a conditional GET request on every page load.

    As for how to determine the last modified date of a given resource... that's really up to you. If you are persisting the resource data in a database, you can create a timestamp column that keeps track of the last update timestamp. You can then use that value for the Last-Modified header.


  2. 2 Posted by Josh Crawford on 30 Apr, 2012 02:50 AM

    Josh Crawford's Avatar

    Hi Josh,

    Thanks for your response, It quite possible that I'm approaching this the wrong way...

    I need to cache the route /totals, unless the database has changed (new values, or updates). This will happen (externally) at various intervals (sometimes a minute, sometimes a day).

    My thought was to create an additional route, /trigger_update, which would reset the timestamp. Until that route were to be requested, the /totals route would always serve cached content.

    $app->get('/totals', function() use ($app) {
        $app->lastModified(1286139652); // this needs to change...
        // logic here
    $app->get('/trigger_update', function() use ($app) {
        $app->lastModified(time()); // this needs to change...

    However, I think I'll include a timestamp value in the database to determine the last modified date.

  3. Andrew Smith closed this discussion on 02 Aug, 2012 10:58 AM.

Comments are currently closed for this discussion. You can start a new one.

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