This is documentation for Kohana v2.3.x.

Table of Contents
StatusDraft
TodoProof read

Expires Helper

Provides methods for managing browser aware page caching. More information on client-side page caching can be found at caching-php-performance

Allows setting a page cache time by sending Last-Modified and Expires headers for the page.

Allows checking if the page is older than the page cache time. If so, the page is expired and a new page must be ouput. If not, a 304 not-modified status header and NO data is sent. The page is retrieved by the browser from it's own cache.

Methods

set()

expires::set() Sets an expiry time for a local, browser page cache. The single parameter is:

Example

expires::set(300);

check()

expires::check() Determines if a cached page needs to be refreshed. The single parameter is.

* [integer] The time in seconds, to add to the last modified time. Default is 60 seconds.

Example

if (expires::check(300) === FALSE)

check_headers()

expires::check_headers() Has no parameters. Called internally by expires::set() Returns boolean TRUE if a Last-Modified or Expires header has NOT been sent.

Example

if (expires::check_headers()) echo 'Safe to send Expires header';

prevent_output()

expires::prevent_output() Has no parameters. Called internally by expires::check() You would not normally call this function directly, as it clears the Kohana output buffer.

Example

expires::prevent_output() // will set Kohana::$output = '';

Full Example

The controller outputs a page from a single method. The objective is to cache the page for ten seconds. If the page is reloaded within ten seconds, the cached page data should be displayed.

Controller:

<?php defined('SYSPATH') or die('No direct script access.');
/**
 * Default Kohana controller.
 */
class Welcome_Controller extends Controller {
 
	public function index()
	{
		if (expires::check(10) === FALSE) expires::set(10);
 
		$welcome = new View('welcome');
 
                // We should only see the time updated in the view after 10 seconds
                // note, it is not this data that is cached, but the browser that fetches a locally cached page
		$welcome->now = date(DATE_RFC822);
 
		$welcome->render(TRUE);
	}
 
}

View:

<h2>Welcome!</h2>
<p>It is now <?php echo $now ?></p>
<hr/>