Book Image

CakePHP 2 Application Cookbook

By : James Watts, Cake Software Foundation, Inc.
Book Image

CakePHP 2 Application Cookbook

By: James Watts, Cake Software Foundation, Inc.

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

Processing Ajax requests


It's no secret that Ajax has revolutionized web development and helped developers build and deliver engaging applications which have a real-time feel to them.

In this recipe, we'll look at how easy it is to handle Ajax requests in CakePHP and give you an introduction to the possibilities at your fingertips.

Getting ready

For this recipe, we'll reuse the books database table from the previous chapter. We'll also need a view for our inventory_index(). action, so create a file named inventory_index.ctp in app/View/Books/.

How to do it...

Perform the following steps:

  1. Add the following beforeFilter() method to the BooksController class:

    public function beforeFilter() {
      parent::beforeFilter();
      if ($this->request->is('ajax')) {
        $this->response->disableCache();
      }
    }
  2. In the same class, add the following inventory_index() method (we will utilize the inventory prefix added in the previous chapter):

    public function inventory_index() {
      $this->set('books', $this...