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
About the Authors
About the Reviewer


With this book, we've aimed to cover and resolve the typically common tasks when working with CakePHP. To do this, we collected the most popular questions from support websites around the Web on how to use certain features or perform common actions with the framework, and ranked them in order of frequency. We then proposed a solution for each of these; once this was done, we put together what we considered to be a comprehensive guide to how to get the job done fast, and right, in each scenario.

While we've done our best to approach each topic with a sound solution, we have been aware of the possibly uneven level of technical knowledge each reader may have. So, where possible, we've tried to keep our examples in line with simple or reasonably understandable parameters so as to reduce any additional knowledge that would potentially be required in each case.

We've dedicated many hours to reviewing each case and hope that you find something useful throughout the scenarios we've outlined. Where it's been obvious, we've relied on basic knowledge of the framework so as to not overburden each tutorial with additional instructions. However, for anyone completely new to the framework, after completing the blog tutorial included in the CakePHP documentation (, you should feel quite comfortable with the assumptions we've made along the way.

What this book covers

Chapter 1, Lightning Introduction, is a set of quick-start recipes to dive head first into using the framework and build out a simple CRUD around product management.

Chapter 2, Advanced Routing, looks at a couple of routing solutions and how to achieve them with the framework.

Chapter 3, HTTP Negotiation, looks at various scenarios where working with HTTP is greatly simplified by CakePHP.

Chapter 4, API Strategies, looks at a couple of ways to expose an API using CakePHP, so you can then decide which fits best with your application.

Chapter 5, Using Authentication, looks at various ways of handling authentication and access control when using the framework.

Chapter 6, Model Layer, looks at the various aspects of models and tells us how to wield their power.

Chapter 7, Search and Pagination, revises some recipes to help you get familiar with Search and Pagination in the framework.

Chapter 8, Events System, looks at the fundamentals of the events system, with a couple of recipes to get you handling events.

Chapter 9, Creating Shells, outlines some common use cases when using shell tasks as well as some built-in shell commands that come with the framework.

Chapter 10, View Templates, looks at various aspects of the view layer in CakePHP and tells us how to get the most out of templates, code reuse, translations, caching, and more.

Chapter 11, Unit Tests, looks at how CakePHP leverages the PHPUnit library to provide a solid base for unit testing, including handling dependencies, fixtures, as well as using the bake shell for testing.

Chapter 12, Migrations, looks at how simple it is to build and maintain your database while keeping your schema changes up to date with your code base.

What you need for this book

You should have comprehensive knowledge of PHP, understanding of the object-oriented nature of the language, as well as the base syntax and language constructs. You will also need a basic understanding of the CakePHP framework, which, for this book, should be enough after having set up and configured the framework to work on a local server (we use a Linux environment throughout the content of the book). You should also have completed at least the blog tutorial included in the CakePHP documentation (

Who this book is for

We'd expect you to have some experience as a PHP developer and some level of knowledge of CakePHP, the solutions it provides, and the features it offers. If you have experience working in other frameworks that follow the MVC architecture, you should also find the content of this book accessible.


In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "Now, create a directory named Products/ in app/View/. Then, in this directory, create one file named index.ctp and another named view.ctp."

A block of code is set as follows:

CREATE TABLE products (
  name VARCHAR(100),
  details TEXT,
  created DATETIME,
  modified DATETIME,

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

public function index() {
  $this->Category->recursive = 0;
  $this->Prg->commonProcess(null, array(
    'paramType' => 'querystring'
  $this->Paginator->settings = array(
    'Category' => array(
      'paramType' => 'querystring',
      'conditions' => $this->Category->parseCriteria($this->Prg->parsedParams())
  $this->set('categories', $this->Paginator->paginate());

Any command-line input or output is written as follows:

$ Console/cake bake all Gift
$ Console/cake bake all Recipient

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "If we fill the search box with the example text and then hit the Submit button."


Warnings or important notes appear in a box like this.


Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.


Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website, or added to any list of existing errata, under the Errata section of that title.


Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.


You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.