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

Dispatch filter


The dispatch filters allow you to intercept the dispatch cycle, run code before or after the controller is built and dispatched, and even return early without completing the full cycle.

In this recipe, we'll create a dispatch filter that exposes an API without requiring the full dispatch cycle.

Getting ready

First, we'll create a file named ApiDispatcher.php in app/Routing/Filter/ as shown in the following code:

<?php
App::uses('DispatcherFilter', 'Routing');
App::uses('ClassRegistry', 'Utility');

class ApiDispatcher extends DispatcherFilter {
}

We'll then need some data to serve from our API. For that, create a libraries table with the following SQL statement:

CREATE TABLE libraries (
  id INT NOT NULL AUTO_INCREMENT,
  name VARCHAR(100),
  details TEXT,
  created DATETIME,
  modified DATETIME,
  PRIMARY KEY(id)
);

Finally, populate the libraries table with the following SQL statement:

INSERT INTO libraries (name, details, created, modified)
VALUES
('Cake', 'Yummy and sweet...