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

Exposing a web service


When you first build an application, you'll most likely imagine how the user will interact with it and how it will be designed and structured for your content and features. However, there also comes a time when you'll want to expose certain functionality of your website as web services.

CakePHP comes well-prepared for this, so in this recipe, we'll look at how you can easily create a data-focused controller to serve certain functionality of your application via a service.

Getting ready

For this recipe, we'll start with a basic controller to serve as our endpoint. So, create a file named ApiController.php in app/Controller/ with the following content:

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

class ApiController extends AppController {
}

How to do it...

Perform the following steps:

  1. Add the following line to your routes.php file in app/Config/:

    Router::connect('/api/:object/:command', array(
      'controller' => 'api',
      'action' => 'delegate'
    ), array(
      'pass...