O'Reilly logo

CakePHP 2 Application Cookbook by Jorge González, James Watts

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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), ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required