Book Image

CakePHP 2 Application Cookbook

Book Image

CakePHP 2 Application Cookbook

Overview of this book

Table of Contents (20 chapters)
CakePHP 2 Application Cookbook
Credits
Foreword
About the Authors
About the Reviewer
www.PacktPub.com
Preface
Index

Cache control


Manipulating the HTTP cache is a great way to control the impact of visitors and users on your application's resources. CakePHP provides a range of options to control both expiration and validation, and manage how clients and proxies engage with your content.

In this recipe, we'll look at the various ways you can handle the HTTP cache to your advantage.

Getting ready

In this recipe, we'll use a simple controller to show each of the caching options available. So, create a file named CacheController.php in app/Controller/, with the following content:

<?php
App::uses('AppController', 'Controller');

class CacheController extends AppController {

  public $autoRender = false;
}

How to do it...

Perform the following steps:

  1. Add a cached() method to the CacheController class, as shown in the following code:

    public function cached() {
      $this->response->cache('-1 hour', '+1 day');
      return __('I am cached!');
    }
  2. In the same class, also add an expire() method:

    public function expire(...