# 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](https://pieceofcake2.github.io/docs/tutorials-and-examples/blog/blog.md)
walks you through a small application step by step.

## About This Fork {#about-this-fork}

This book documents [pieceofcake2/cakephp](https://github.com/pieceofcake2/cakephp),
a community-maintained fork of CakePHP 2.x. The fork is based on
[cakephp/cakephp](https://github.com/cakephp/cakephp/tree/2.10.24) 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](https://bakery.cakephp.org/2021/10/02/cakephp_2_eol.html).

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](https://pieceofcake2.github.io/docs/installation/application-skeleton.md).
[Changes from the Original Book](https://pieceofcake2.github.io/docs/appendices/changes-from-the-original-book.md)
lists how it differs from the original CakePHP 2.x documentation. The
[Piece of Cake 2 website](https://pieceofcake2.github.io/) links to the
fork's other projects and resources. Where each of these packages comes from
is listed in [Maintained Plugins](https://pieceofcake2.github.io/docs/plugins/maintained-plugins.md#origins).

## What's Different from CakePHP 2.10 {#whats-different}

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/cakephp` with Composer; archive,
  PEAR and `include_path` installations are gone. The package replaces
  `cakephp/cakephp`, so plugins that require `cakephp/cakephp` 2.x keep
  installing. The framework lives in `vendor/pieceofcake2/cakephp/src/`
  (formerly `lib/Cake/`), and Composer loads its classes instead of
  `App::uses()`. See [Installation](https://pieceofcake2.github.io/docs/installation.md) and
  [Autoloading](https://pieceofcake2.github.io/docs/development/autoloading.md).
- **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 `String` class (use
  `CakeText`) and the Xcache and Wincache cache engines. See
  [Migrating from CakePHP 2.10](https://pieceofcake2.github.io/docs/appendices/migrating-from-cakephp-2-10.md).
- **Modern directory layout.** Applications can use the layout of CakePHP 5
  (`bin/cake`, `config/`, `src/`, `templates/`, `tests/`, `plugins/`), and
  the legacy `app/` layout keeps working. This book uses the modern layout. See
  [CakePHP Folder Structure](https://pieceofcake2.github.io/docs/getting-started/cakephp-folder-structure.md).
- **PHPUnit 9.6.** Tests run on PHPUnit 9.6 instead of 3.7. See
  [Testing](https://pieceofcake2.github.io/docs/development/testing.md) and
  [Migrating Tests to PHPUnit 9.6](https://pieceofcake2.github.io/docs/appendices/phpunit-9-migration-hints.md).
- **Coding standard.** The fork and the samples in this book follow the
  CakePHP 5 codesniffer rules (4 spaces, short array syntax). See
  [Coding Standards](https://pieceofcake2.github.io/docs/contributing/cakephp-coding-conventions.md).
- **Static analysis.** The
  [pieceofcake2/phpstan-cakephp2](https://pieceofcake2.github.io/phpstan-cakephp2/)
  extension lets PHPStan understand models, components and behaviors. See
  [Static Analysis](https://pieceofcake2.github.io/docs/development/static-analysis.md).
- **Security fixes.** Known vulnerabilities of CakePHP 2.10.24 are fixed, for
  example CSRF tokens are now HMAC-signed. See
  [Security Fixes](https://pieceofcake2.github.io/docs/security/fixes.md).
- **Backports from CakePHP 5.** `IntegrationTestTrait` and `EmailTrait`
  (CakePHP 2.12.4) test controllers through the real dispatcher with the
  CakePHP 5 method names, see
  [Integration Testing](https://pieceofcake2.github.io/docs/development/integration-testing.md). The application
  skeleton loads `.env` files (pieceofcake2/app 2.4.0), see [Environment Variables](https://pieceofcake2.github.io/docs/development/configuration.md#environment-variables).
  In debug mode, the core error pages use the CakePHP 5 layout with code
  excerpts and editor links, see
  [Debug Error Pages](https://pieceofcake2.github.io/docs/development/debugging.md#debug-error-pages), and the
  `utf8` validation rule rejects invalid UTF-8, see
  [Data Validation](https://pieceofcake2.github.io/docs/models/data-validation.md) (both CakePHP 2.12.6).
  Datasources and email profiles can be configured with a DSN from
  `DATABASE_URL` and `MAILER_DSN`, see
  [Database DSNs](https://pieceofcake2.github.io/docs/development/configuration.md#database-url) (CakePHP 2.12.7).
  Views mark the HTML that they print without escaping on purpose with
  `raw()`, see [raw()](https://pieceofcake2.github.io/docs/core-libraries/global-constants-and-functions.md#raw)
  (CakePHP 2.12.10).
- **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. Plugins
  can use the `src/` and `templates/` layout of CakePHP 5 (CakePHP 2.13). See
  [Code Generation with Bake](https://pieceofcake2.github.io/docs/console-and-shells/code-generation-with-bake.md) and
  [Maintained Plugins](https://pieceofcake2.github.io/docs/plugins/maintained-plugins.md).

## Where to Start {#where-to-start}

### Existing Application {#existing-application}

Most readers of this book maintain an application that already runs on
CakePHP 2:

1. Read [Migrating from CakePHP 2.10](https://pieceofcake2.github.io/docs/appendices/migrating-from-cakephp-2-10.md)
   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.
2. Replace `cakephp/cakephp` with `pieceofcake2/cakephp` and update the
   dispatcher files, as described in
   [Adding CakePHP to an Existing Application](https://pieceofcake2.github.io/docs/installation.md#adding-cakephp-to-an-existing-application).
3. Optionally, move the application to the modern layout one directory at a
   time, as described in
   [Migrating to the Modern Layout](https://pieceofcake2.github.io/docs/appendices/migrating-to-the-modern-layout.md).
   This step also prepares the application for a later move to CakePHP 5.

### New Application {#new-application}

To start an application on the fork, create it from the application skeleton:

```bash
composer create-project pieceofcake2/app my_app
```

See [Creating a New Application](https://pieceofcake2.github.io/docs/installation.md#creating-a-new-application) for
the configuration that follows.

> [!NOTE]
> For a new project, consider [CakePHP 5](https://book.cakephp.org/5/en/)
> first. The fork is meant for applications that need to stay on CakePHP 2.

## Coming from Modern PHP {#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:

- **Global class names.** Application classes are global classes, and the
  class name and the file location follow
  [conventions](https://pieceofcake2.github.io/docs/getting-started/cakephp-conventions.md#file-and-classname-conventions),
  for example `PostsController` in `src/Controller/PostsController.php`. On the
  fork, Composer's classmap loads them, and `App::uses()` is deprecated
  (CakePHP 2.13). The framework classes
  are in the `Cake` namespace with their CakePHP 2 names as aliases, such as
  `Controller` for `Cake\Controller\Controller` (CakePHP 2.13). Your own
  classes can use namespaces; [Namespaced Classes](https://pieceofcake2.github.io/docs/development/namespaced-classes.md) lists where CakePHP
  accepts them in its configuration, and how `App.namespace` finds controllers
  and models in your namespace.
- **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](https://pieceofcake2.github.io/docs/models/retrieving-your-data.md). Form data, validation
  rules and component settings are arrays as well.
- **Models are table gateways.** One `Model` class 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](https://pieceofcake2.github.io/docs/getting-started/cakephp-structure.md).
- **Convention-based wiring, not dependency injection.** There is no service
  container. Controllers declare what they use in properties such as
  `$uses`, `$components` and `$helpers`, and access them as
  `$this->Post`, `$this->Auth` or `$this->Html`.
- **Global configuration.** Settings live in a static registry,
  [Configure](https://pieceofcake2.github.io/docs/development/configuration.md#configure-class), and are read with
  `Configure::read('App.encoding')`. Caches and logs are configured with
  static calls in the same style, `Cache::config()` and `CakeLog::config()`.
- **No PSR-7 or middleware.** Requests and responses are `CakeRequest` and
  `CakeResponse` objects, and code that runs around the dispatcher is written as
  [dispatcher filters](https://pieceofcake2.github.io/docs/development/dispatch-filters.md).
- **Few native types.** Most core methods have no parameter or return type
  declarations. Docblocks and [static analysis](https://pieceofcake2.github.io/docs/development/static-analysis.md)
  fill the gap.

[A Typical CakePHP Request](https://pieceofcake2.github.io/docs/getting-started/a-typical-cakephp-request.md) shows
how these pieces work together.

## In This Section

- [Installation](https://pieceofcake2.github.io/docs/installation.md),
  [Advanced Installation](https://pieceofcake2.github.io/docs/installation/advanced-installation.md) and
  [Web Server Configuration](https://pieceofcake2.github.io/docs/installation/url-rewriting.md)
- [CakePHP Folder Structure](https://pieceofcake2.github.io/docs/getting-started/cakephp-folder-structure.md)
- [CakePHP Conventions](https://pieceofcake2.github.io/docs/getting-started/cakephp-conventions.md)
- [CakePHP Structure](https://pieceofcake2.github.io/docs/getting-started/cakephp-structure.md)
- [A Typical CakePHP Request](https://pieceofcake2.github.io/docs/getting-started/a-typical-cakephp-request.md)
