Getting Started
This page is the starting point of the book for readers who already run a CakePHP 2 application, and for developers who know modern PHP and have to work on one. It summarizes what is different on this fork and where to read more. If you are new to CakePHP 2, the Blog Tutorial walks you through a small application step by step.
About This Fork
This book documents pieceofcake2/cakephp, a community-maintained fork of CakePHP 2.x. The fork is based on CakePHP 2.10.24 and runs on PHP 8.0 to 8.5. It is maintenance-focused: it keeps CakePHP 2 compatible with current PHP versions and fixes bugs and security issues. It does not aim to add features; additions are limited to what helps maintenance and migration, such as backports from CakePHP 5.
The CakePHP project ended support for CakePHP 2.x in June 2021.
This cookbook describes CakePHP 2 as it works on the fork: installation with Composer, PHP 8, and the modern directory layout used by pieceofcake2/app. Changes from the Original Book lists how it differs from the original CakePHP 2.x documentation. The Piece of Cake 2 website links to the fork's other projects and resources.
What's Different from CakePHP 2.10
If you know CakePHP 2.10, most of your knowledge still applies: the models, controllers, views, components, behaviors and helpers work as before. These are the points that changed:
- Composer only. Install
pieceofcake2/cakephpwith Composer; archive, PEAR andinclude_pathinstallations are gone. The package replacescakephp/cakephp, so plugins that requirecakephp/cakephp2.x keep installing. The framework lives invendor/pieceofcake2/cakephp/src/Cake/(formerlylib/Cake/), and its classes are loaded by Composer's classmap instead ofApp::uses(). See Installation and Loading classes. - PHP 8. The fork runs on PHP 8.0 to 8.5. A few APIs that do not work on current PHP versions were removed, for example the
Stringclass (useCakeText) and the Xcache and Wincache cache engines. See Migrating from CakePHP 2.10. - Modern directory layout. Applications can use the layout of CakePHP 5 (
bin/cake,config/,src/,templates/,tests/,plugins/), and the legacyapp/layout keeps working. This book uses the modern layout. See CakePHP Folder Structure. - PHPUnit 9.6. Tests run on PHPUnit 9.6 instead of 3.7. See Testing and Migrating Tests to PHPUnit 9.6.
- Coding standard. The fork and the samples in this book follow the CakePHP 5 codesniffer rules (4 spaces, short array syntax). See Coding Standards.
- Static analysis. The pieceofcake2/phpstan-cakephp2 extension lets PHPStan understand models, components and behaviors. See Static Analysis.
- Security fixes. Known vulnerabilities of CakePHP 2.10.24 are fixed, for example CSRF tokens are now HMAC-signed. See Security Fixes.
- Backports from CakePHP 5.
IntegrationTestTraitandEmailTrait(CakePHP 2.12.4) test controllers through the real dispatcher with the CakePHP 5 method names, see the upgrade guide of the fork. The application skeleton loads.envfiles (pieceofcake2/app 2.4.0), see Environment Variables. - Bake and plugins. Bake and the upgrade shell moved out of the core into plugins, and the major CakePHP 2 plugins are maintained for PHP 8. See Code Generation with Bake and Maintained Plugins.
Where to Start
Existing Application
Most readers of this book maintain an application that already runs on CakePHP 2:
- Read Migrating from CakePHP 2.10 for the prerequisites and the breaking changes. The fork is based on CakePHP 2.10.24, so upgrade an older 2.x application to 2.10.24 first.
- Replace
cakephp/cakephpwithpieceofcake2/cakephpand update the dispatcher files, as described in Adding CakePHP to an Existing Application. - Optionally, move the application to the modern layout one directory at a time, as described in Migrating to the Modern Layout. This step also prepares the application for a later move to CakePHP 5.
New Application
To start an application on the fork, create it from the application skeleton:
composer create-project pieceofcake2/app my_appSee Creating a New Application for the configuration that follows.
NOTE
For a new project, consider CakePHP 5 first. The fork is meant for applications that need to stay on CakePHP 2.
Coming from Modern PHP
CakePHP 2 was designed for PHP 5.2 and 5.3. If you know CakePHP 5, Laravel or Symfony, expect these differences:
- No namespaces. Framework and application classes are global classes, and the class name and the file location follow conventions, for example
PostsControllerinsrc/Controller/PostsController.php. On the fork, Composer's classmap loads them;App::uses()is only needed for classes Composer does not know, such as most plugin classes. - Arrays instead of objects.
find()takes an array of options and returns nested arrays keyed by model alias, such as$post['Post']['title'], not entities or collections. See Retrieving Your Data. Form data, validation rules and component settings are arrays as well. - Models are table gateways. One
Modelclass per table handles finding, saving, validation and associations. Reusable model logic goes into behaviors, controller logic into components and view logic into helpers. See CakePHP Structure. - Convention-based wiring, not dependency injection. There is no service container. Controllers declare what they use in properties such as
$uses,$componentsand$helpers, and access them as$this->Post,$this->Author$this->Html. - Global configuration. Settings live in a static registry, Configure, and are read with
Configure::read('App.encoding'). Caches and logs are configured with static calls in the same style,Cache::config()andCakeLog::config(). - No PSR-7 or middleware. Requests and responses are
CakeRequestandCakeResponseobjects, and code that runs around the dispatcher is written as dispatcher filters. - Few native types. Most core methods have no parameter or return type declarations. Docblocks and static analysis fill the gap.
A Typical CakePHP Request shows how these pieces work together.
