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

Building a response


CakePHP is designed to take most of the work out of building applications. However, sometimes you may want to handle things yourself, which the framework can also facilitate.

In this recipe we'll process a response ourselves, generating it exactly as we want.

Getting ready

As this recipe is straight to the point, we'll simply enhance an existing example controller to build our response. For this, open a file named ExampleController.php in app/Controller/ and add the following respond() method to the ExampleController class:

public function respond() {
  if (!$this->request->query('text')) {
    $this->response->statusCode(404);
  }
  $this->response->header('X-Timestamp', date('H:i:s'));
  $this->response->type('txt');
  $this->response->body($this->request->query('text'));
  $this->response->send();
  $this->_stop();
}

Navigate to /example/respond?text=Hello in your browser, which should output Hello, as shown in the following...